diff --git a/mypy/build.py b/mypy/build.py index 0941f6d6943c..dd923652a264 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -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.nodes import Import, ImportAll, ImportBase, ImportFrom, MypyFile, SymbolTable @@ -241,6 +242,32 @@ def _build( ) 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) diff --git a/mypy/main.py b/mypy/main.py index c8194db00cfd..3586ab9e2ca4 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1255,14 +1255,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