From 3f49da0caff40e814bf57d065ed4e800c56510d0 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 30 Aug 2023 22:35:12 -0700 Subject: [PATCH 1/3] Fix crash when parsing error code config with typo Fixes #16002 --- mypy/config_parser.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mypy/config_parser.py b/mypy/config_parser.py index a84f3594a0d2..ed98cd8ae5f9 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -439,6 +439,11 @@ def parse_section( options_key = key if key in config_types: ct = config_types[key] + elif key in ("enabled_error_codes", "disabled_error_codes"): + # These should be "enable_error_code" and "disable_error_code". But because these fields + # exist on Options, we would otherwise accept them here and crash later. + print(f"{prefix}Unrecognized option: {key} = {section[key]}", file=stderr) + continue else: dv = None # We have to keep new_semantic_analyzer in Options From af9231297f8c608abd0a6cdf8a621263a47048a7 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 1 Sep 2023 00:31:57 -0700 Subject: [PATCH 2/3] suggest --- mypy/config_parser.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mypy/config_parser.py b/mypy/config_parser.py index ed98cd8ae5f9..9ca7243e62eb 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -442,7 +442,15 @@ def parse_section( elif key in ("enabled_error_codes", "disabled_error_codes"): # These should be "enable_error_code" and "disable_error_code". But because these fields # exist on Options, we would otherwise accept them here and crash later. - print(f"{prefix}Unrecognized option: {key} = {section[key]}", file=stderr) + suggestion = { + "enabled_error_codes": "enable_error_code", + "disabled_error_codes": "disable_error_code", + } + print( + f"{prefix}Unrecognized option: {key} = {section[key]}" + f" (did you mean {suggestion[key]}?)", + file=stderr, + ) continue else: dv = None From c69ec14011d2187e0591f73150338dfb17da396f Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 1 Sep 2023 13:10:53 -0700 Subject: [PATCH 3/3] nit --- mypy/config_parser.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mypy/config_parser.py b/mypy/config_parser.py index 9ca7243e62eb..7748c3b25966 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -434,21 +434,23 @@ def parse_section( """ results: dict[str, object] = {} report_dirs: dict[str, str] = {} + + # Because these fields exist on Options, without proactive checking, we would accept them + # and crash later + invalid_options = { + "enabled_error_codes": "enable_error_code", + "disabled_error_codes": "disable_error_code", + } + for key in section: invert = False options_key = key if key in config_types: ct = config_types[key] - elif key in ("enabled_error_codes", "disabled_error_codes"): - # These should be "enable_error_code" and "disable_error_code". But because these fields - # exist on Options, we would otherwise accept them here and crash later. - suggestion = { - "enabled_error_codes": "enable_error_code", - "disabled_error_codes": "disable_error_code", - } + elif key in invalid_options: print( f"{prefix}Unrecognized option: {key} = {section[key]}" - f" (did you mean {suggestion[key]}?)", + f" (did you mean {invalid_options[key]}?)", file=stderr, ) continue