Skip to content

Commit

Permalink
Allow to enable/disable plugin error codes
Browse files Browse the repository at this point in the history
Fixes python#12987

* Ignore unknown error codes in process_options
* Reload enabled / disabled error codes after loading plugins
* Unknown errors codes no longer a hard failure, they only get logged to
  stderr
  • Loading branch information
tik-stbuehler authored and scottp-dpaw committed Oct 3, 2023
1 parent 3534bac commit c5d70a2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
27 changes: 27 additions & 0 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import mypy.semanal_main
from mypy.checker import TypeChecker
from mypy.errorcodes import error_codes
from mypy.errors import CompileError, ErrorInfo, Errors, report_internal_error
from mypy.indirection import TypeIndirectionVisitor
from mypy.messages import MessageBuilder
Expand Down Expand Up @@ -241,6 +242,32 @@ def _build(
errors = Errors(options, read_source=lambda path: read_py_file(path, cached_read))
plugin, snapshot = load_plugins(options, errors, stdout, extra_plugins)

# Plugins might define own error codes, take a look again now

# Process `--enable-error-code` and `--disable-error-code` flags
disabled_codes = set(options.disable_error_code)
enabled_codes = set(options.enable_error_code)

valid_error_codes = set(error_codes.keys())
invalid_codes = (enabled_codes | disabled_codes) - valid_error_codes
if invalid_codes:
print(f"Invalid error code(s): {', '.join(sorted(invalid_codes))}", file=stderr)

options.disabled_error_codes |= {
error_codes[code] for code in disabled_codes if code in error_codes
}
options.enabled_error_codes |= {
error_codes[code] for code in enabled_codes if code in error_codes
}

# Enabling an error code always overrides disabling
options.disabled_error_codes -= options.enabled_error_codes

# Update error codes in Errors object (might use a different
# set instance if original was empty)
errors.disabled_error_codes = options.disabled_error_codes
errors.enabled_error_codes = options.enabled_error_codes

# Add catch-all .gitignore to cache dir if we created it
cache_dir_existed = os.path.isdir(options.cache_dir)

Expand Down
15 changes: 7 additions & 8 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1302,14 +1302,13 @@ def set_strict_flags() -> None:
disabled_codes = set(options.disable_error_code)
enabled_codes = set(options.enable_error_code)

valid_error_codes = set(error_codes.keys())

invalid_codes = (enabled_codes | disabled_codes) - valid_error_codes
if invalid_codes:
parser.error(f"Invalid error code(s): {', '.join(sorted(invalid_codes))}")

options.disabled_error_codes |= {error_codes[code] for code in disabled_codes}
options.enabled_error_codes |= {error_codes[code] for code in enabled_codes}
# Plugins might define own error codes later, only load those we already know about here
options.disabled_error_codes |= {
error_codes[code] for code in disabled_codes if code in error_codes
}
options.enabled_error_codes |= {
error_codes[code] for code in enabled_codes if code in error_codes
}

# Enabling an error code always overrides disabling
options.disabled_error_codes -= options.enabled_error_codes
Expand Down

0 comments on commit c5d70a2

Please sign in to comment.