Skip to content

Add option for strict-error-codes #13541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,14 @@ of the above sections.

assert text is not None # OK, check against None is allowed as a special case.

.. option:: --strict-error-codes

Enable additional error codes, disabled by default. You can see the list
of codes enabled by this flag in the full :option:`mypy --help` output.

Note: the exact list of codes enabled by running :option:`--strict-error-codes`
may change over time.

.. option:: --strict

This flag mode enables all optional error checking flags. You can see the
Expand Down
11 changes: 11 additions & 0 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,17 @@ section of the command line docs.
Prohibit equality checks, identity checks, and container checks between
non-overlapping types.

.. confval:: strict_error_codes

:type: boolean
:default: False

Enable additional error codes, disabled by default. You can see the list
of codes enabled by this flag in the full :option:`mypy --help` output.

Note: the exact list of codes enabled by running :confval:`strict_error_codes`
may change over time.

.. confval:: strict

:type: boolean
Expand Down
10 changes: 9 additions & 1 deletion mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@

class ErrorCode:
def __init__(
self, code: str, description: str, category: str, default_enabled: bool = True
self,
code: str,
description: str,
category: str,
default_enabled: bool = True,
*,
strict_enabled: bool = False,
) -> None:
self.code = code
self.description = description
self.category = category
self.default_enabled = default_enabled
self.strict_enabled = strict_enabled
error_codes[code] = self

def __str__(self) -> str:
Expand Down Expand Up @@ -144,6 +151,7 @@ def __str__(self) -> str:
"Warn about '# type: ignore' comments which do not have error codes",
"General",
default_enabled=False,
strict_enabled=True,
)
UNUSED_AWAITABLE: Final = ErrorCode(
"unused-awaitable",
Expand Down
4 changes: 4 additions & 0 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,18 @@ def is_error_code_enabled(self, error_code: ErrorCode) -> bool:
if self.options:
current_mod_disabled = self.options.disabled_error_codes
current_mod_enabled = self.options.enabled_error_codes
strict_error_codes = self.options.strict_error_codes
else:
current_mod_disabled = set()
current_mod_enabled = set()
strict_error_codes = False

if error_code in current_mod_disabled:
return False
elif error_code in current_mod_enabled:
return True
elif strict_error_codes and error_code.strict_enabled:
return True
else:
return error_code.default_enabled

Expand Down
14 changes: 14 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,20 @@ def add_invertible_flag(
group=strictness_group,
)

strict_error_code_names = [name for name, code in error_codes.items() if code.strict_enabled]
strict_error_codes_help = (
"Enable additional error codes, disabled by default; "
f"enables the following codes: {', '.join(strict_error_code_names)}"
)

add_invertible_flag(
"--strict-error-codes",
default=False,
strict_flag=True,
help=strict_error_codes_help,
group=strictness_group,
)

strict_help = "Strict mode; enables the following flags: {}".format(
", ".join(strict_flag_names)
)
Expand Down
2 changes: 2 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ def __init__(self) -> None:
# Variable names considered False
self.always_false: list[str] = []

self.strict_error_codes = False

# Error codes to disable
self.disable_error_code: list[str] = []
self.disabled_error_codes: set[ErrorCode] = set()
Expand Down
4 changes: 4 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -925,3 +925,7 @@ def f(d: D, s: str) -> None:
[case testRecommendErrorCode]
# type: ignore[whatever] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code=...` [syntax]
1 + "asdf"

[case testStrictErrorCodes]
# flags: --strict-error-codes
var: bool = 1 # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[assignment]" instead) [ignore-without-code]