Skip to content

Commit 30aafba

Browse files
Implement disable ignores, a flag which disables all type ignores
This is available as a command-line flag and a global configuration option. This commit also includes documentation and tests for this feature. Fixes #13201
1 parent 19697af commit 30aafba

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

docs/source/command_line.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,12 @@ of the above sections.
866866
x = 'a string'
867867
x.trim() # error: "str" has no attribute "trim" [attr-defined]
868868
869+
.. option:: --disable-ignores
870+
871+
This flag makes mypy act as though no ``# type: ignore`` comments are placed inline.
872+
Effectively, it makes mypy "ignore ignores", therefore reporting all of the errors that would otherwise be ignored by those comments.
873+
Beware: other configuration options and flags may still enable or disable various error codes.
874+
The main use of this flag is to audit code that has ``# type: ignore`` comments in it (potentially masking problems in a subtle way).
869875

870876
.. _configuring-error-messages:
871877

docs/source/config_file.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,14 @@ section of the command line docs.
800800

801801
Note: This option will override disabled error codes from the disable_error_code option.
802802

803+
.. confval:: disable_ignores
804+
805+
:type: boolean
806+
:default: false
807+
:availability: global
808+
809+
Ignores ignores. See :option:`mypy --disable-ignores`.
810+
803811
.. confval:: extra_checks
804812

805813
:type: boolean

mypy/fastparse.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -896,12 +896,13 @@ def translate_module_id(self, id: str) -> str:
896896

897897
def visit_Module(self, mod: ast3.Module) -> MypyFile:
898898
self.type_ignores = {}
899-
for ti in mod.type_ignores:
900-
parsed = parse_type_ignore_tag(ti.tag)
901-
if parsed is not None:
902-
self.type_ignores[ti.lineno] = parsed
903-
else:
904-
self.fail(message_registry.INVALID_TYPE_IGNORE, ti.lineno, -1, blocker=False)
899+
if not self.options.disable_ignores:
900+
for ti in mod.type_ignores:
901+
parsed = parse_type_ignore_tag(ti.tag)
902+
if parsed is not None:
903+
self.type_ignores[ti.lineno] = parsed
904+
else:
905+
self.fail(message_registry.INVALID_TYPE_IGNORE, ti.lineno, -1, blocker=False)
905906

906907
body = self.fix_function_overloads(self.translate_stmt_list(mod.body, ismodule=True))
907908

mypy/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,10 @@ def add_invertible_flag(
956956
help="Enable a specific error code",
957957
)
958958

959+
strictness_group.add_argument(
960+
"--disable-ignores", action="store_true", help="Don't honor '# type: ignore' comments"
961+
)
962+
959963
error_group = parser.add_argument_group(
960964
title="Configuring error messages",
961965
description="Adjust the amount of detail shown in error messages.",

mypy/options.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ def __init__(self) -> None:
262262
self.enable_error_code: list[str] = []
263263
self.enabled_error_codes: set[ErrorCode] = set()
264264

265+
# Disable all type ignore comments
266+
self.disable_ignores = False
267+
265268
# Use script name instead of __main__
266269
self.scripts_are_modules = False
267270

test-data/unit/check-flags.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ def f(x): pass
44
[out]
55
main:2: error: Function is missing a type annotation
66

7+
[case testDisableIgnores]
8+
# flags: --disable-ignores
9+
# type: ignore # a type ignore before everything usually ignores the entire file. But not now!
10+
a # E: Name "a" is not defined
11+
a #type: ignore # E: Name "a" is not defined
12+
a #type: ignore[golly] # E: Name "a" is not defined
13+
a #type: ignore[name-defined] # E: Name "a" is not defined
14+
15+
[case testDisableIgnoresConfig]
16+
# flags: --config-file tmp/mypy.ini
17+
# type: ignore # a type ignore before everything usually ignores the entire file. But not now!
18+
a # E: Name "a" is not defined
19+
a #type: ignore # E: Name "a" is not defined
20+
a #type: ignore[golly] # E: Name "a" is not defined
21+
a #type: ignore[name-defined] # E: Name "a" is not defined
22+
[file mypy.ini]
23+
\[mypy]
24+
disable_ignores=True
25+
726
[case testUnannotatedArgument]
827
# flags: --disallow-untyped-defs
928
def f(x) -> int: pass

0 commit comments

Comments
 (0)