From 9507ae2865d37d5f590d8a506240b3d0bcea04ea Mon Sep 17 00:00:00 2001 From: Cibin Mathew Date: Wed, 16 Aug 2023 00:12:37 +0200 Subject: [PATCH 1/2] Fix: Exit if any config error --- mypy/build.py | 1 + mypy/config_parser.py | 17 ++++++++++------- mypy/main.py | 6 +++++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index 39629c2dc4557..6a16b8142e98c 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -2207,6 +2207,7 @@ def parse_inline_configuration(self, source: str) -> None: """Check for inline mypy: options directive and parse them.""" flags = get_mypy_comments(source) if flags: + # TODO: should this be captured as well for parser errors? changes, config_errors = parse_mypy_comments(flags, self.options) self.options = self.options.apply_changes(changes) self.manager.errors.set_file(self.xpath, self.id, self.options) diff --git a/mypy/config_parser.py b/mypy/config_parser.py index 4dbd6477c81e8..0bc8463040411 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -471,8 +471,8 @@ def parse_section( if report_type in defaults.REPORTER_NAMES: report_dirs[report_type] = str(section[key]) else: - print(f"{prefix}Unrecognized report type: {key}", file=stderr) - continue + msg = f"{prefix}Unrecognized report type: {key}" + raise argparse.ArgumentTypeError(msg) if key.startswith("x_"): pass # Don't complain about `x_blah` flags elif key.startswith("no_") and hasattr(template, key[3:]): @@ -490,7 +490,8 @@ def parse_section( elif key == "strict": pass # Special handling below else: - print(f"{prefix}Unrecognized option: {key} = {section[key]}", file=stderr) + msg = f"{prefix}Unrecognized option: {key} = {section[key]}" + raise argparse.ArgumentTypeError(msg) if invert: dv = getattr(template, options_key, None) else: @@ -507,19 +508,21 @@ def parse_section( v = not v elif callable(ct): if invert: + # TODO: should this be captured or it is okay to ignore print(f"{prefix}Can not invert non-boolean key {options_key}", file=stderr) continue try: v = ct(section.get(key)) except argparse.ArgumentTypeError as err: - print(f"{prefix}{key}: {err}", file=stderr) - continue + msg = f"{prefix}{key}: {err}" + raise argparse.ArgumentTypeError(msg) from err else: print(f"{prefix}Don't know what type {key} should have", file=stderr) + # TODO: should this be captured or it is okay to ignore as type is not known continue except ValueError as err: - print(f"{prefix}{key}: {err}", file=stderr) - continue + msg = f"{prefix}{key}: {err}" + raise ValueError(msg) from err if key == "strict": if v: set_strict_flags() diff --git a/mypy/main.py b/mypy/main.py index 3eb8a76a6de3f..bbfd2fde2eeb0 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1248,7 +1248,11 @@ def set_strict_flags() -> None: setattr(options, dest, value) # Parse config file first, so command line can override. - parse_config_file(options, set_strict_flags, config_file, stdout, stderr) + try: + parse_config_file(options, set_strict_flags, config_file, stdout, stderr) + except (argparse.ArgumentTypeError, ValueError) as err: + stderr.write(f"{err}") + sys.exit(1) # Set strict flags before parsing (if strict mode enabled), so other command # line options can override. From 44a8eb005c916f4a28439fff0c155c9260b24b12 Mon Sep 17 00:00:00 2001 From: Cibin Mathew Date: Thu, 17 Aug 2023 00:55:55 +0200 Subject: [PATCH 2/2] Review feedbacks --- mypy/build.py | 1 - mypy/config_parser.py | 4 +--- mypy/main.py | 3 +-- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index 6a16b8142e98c..39629c2dc4557 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -2207,7 +2207,6 @@ def parse_inline_configuration(self, source: str) -> None: """Check for inline mypy: options directive and parse them.""" flags = get_mypy_comments(source) if flags: - # TODO: should this be captured as well for parser errors? changes, config_errors = parse_mypy_comments(flags, self.options) self.options = self.options.apply_changes(changes) self.manager.errors.set_file(self.xpath, self.id, self.options) diff --git a/mypy/config_parser.py b/mypy/config_parser.py index 0bc8463040411..ca678d3b4c7a7 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -473,7 +473,7 @@ def parse_section( else: msg = f"{prefix}Unrecognized report type: {key}" raise argparse.ArgumentTypeError(msg) - if key.startswith("x_"): + elif key.startswith("x_"): pass # Don't complain about `x_blah` flags elif key.startswith("no_") and hasattr(template, key[3:]): options_key = key[3:] @@ -508,7 +508,6 @@ def parse_section( v = not v elif callable(ct): if invert: - # TODO: should this be captured or it is okay to ignore print(f"{prefix}Can not invert non-boolean key {options_key}", file=stderr) continue try: @@ -518,7 +517,6 @@ def parse_section( raise argparse.ArgumentTypeError(msg) from err else: print(f"{prefix}Don't know what type {key} should have", file=stderr) - # TODO: should this be captured or it is okay to ignore as type is not known continue except ValueError as err: msg = f"{prefix}{key}: {err}" diff --git a/mypy/main.py b/mypy/main.py index bbfd2fde2eeb0..f3c3993220cf0 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1251,8 +1251,7 @@ def set_strict_flags() -> None: try: parse_config_file(options, set_strict_flags, config_file, stdout, stderr) except (argparse.ArgumentTypeError, ValueError) as err: - stderr.write(f"{err}") - sys.exit(1) + raise SystemExit(str(err)) from err # Set strict flags before parsing (if strict mode enabled), so other command # line options can override.