Skip to content

Commit

Permalink
Fix: Exit if any config error
Browse files Browse the repository at this point in the history
  • Loading branch information
cibinmathew committed Aug 22, 2023
1 parent 7141d6b commit ab536c5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 10 additions & 7 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]):
Expand All @@ -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:
Expand All @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ab536c5

Please sign in to comment.