Skip to content

Commit da56c97

Browse files
authored
Warn on module level type ignore with error code (#13512)
Per-module error codes were added in #13502, let's recommend using them. The existing type ignore behaviour is pretty unintuitive; I think most people actually want `# mypy: ignore-errors`. There are probably people depending on the current behaviour though. Fixes #13435, fixes #12076, fixes #11999, fixes #11027, fixes #9318, fixes #7839
1 parent d7c0bb5 commit da56c97

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

docs/source/common_issues.rst

+7-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,13 @@ Ignoring a whole file
187187
---------------------
188188

189189
A ``# type: ignore`` comment at the top of a module (before any statements,
190-
including imports or docstrings) has the effect of ignoring the *entire* module.
190+
including imports or docstrings) has the effect of ignoring the entire contents of the module.
191+
192+
To only ignore errors, use a top-level ``# mypy: ignore-errors`` comment instead.
193+
To only ignore errors with a specific error code, use a top-level
194+
``# mypy: disable-error-code=...`` comment.
195+
To replace the contents of the module with ``Any``, use a per-module ``follow_imports = skip``.
196+
See :ref:`Following imports <follow-imports>` for details.
191197

192198
.. code-block:: python
193199

mypy/fastparse.py

+10
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,16 @@ def translate_stmt_list(
483483
and self.type_ignores
484484
and min(self.type_ignores) < self.get_lineno(stmts[0])
485485
):
486+
if self.type_ignores[min(self.type_ignores)]:
487+
self.fail(
488+
(
489+
"type ignore with error code is not supported for modules; "
490+
"use `# mypy: disable-error-code=...`"
491+
),
492+
line=min(self.type_ignores),
493+
column=0,
494+
blocker=False,
495+
)
486496
self.errors.used_ignored_lines[self.errors.file][min(self.type_ignores)].append(
487497
codes.FILE.code
488498
)

test-data/unit/check-errorcodes.test

+5
Original file line numberDiff line numberDiff line change
@@ -920,3 +920,8 @@ def f(d: D, s: str) -> None:
920920
d[s] # type: ignore[literal-required]
921921
[builtins fixtures/dict.pyi]
922922
[typing fixtures/typing-typeddict.pyi]
923+
924+
925+
[case testRecommendErrorCode]
926+
# type: ignore[whatever] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code=...` [syntax]
927+
1 + "asdf"

0 commit comments

Comments
 (0)