diff --git a/mypy/build.py b/mypy/build.py index 525d5f436e7e1..f8a9294ebd766 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 a84f3594a0d22..f6c889c9de96b 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -452,8 +452,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:]): @@ -471,7 +471,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: @@ -488,19 +489,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 30f6cfe97455d..329bfe51c061f 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1249,7 +1249,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.