Skip to content

Commit

Permalink
Warn on module level type ignore with error code (#13512)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hauntsaninja committed Aug 26, 2022
1 parent d7c0bb5 commit da56c97
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion docs/source/common_issues.rst
Expand Up @@ -187,7 +187,13 @@ Ignoring a whole file
---------------------

A ``# type: ignore`` comment at the top of a module (before any statements,
including imports or docstrings) has the effect of ignoring the *entire* module.
including imports or docstrings) has the effect of ignoring the entire contents of the module.

To only ignore errors, use a top-level ``# mypy: ignore-errors`` comment instead.
To only ignore errors with a specific error code, use a top-level
``# mypy: disable-error-code=...`` comment.
To replace the contents of the module with ``Any``, use a per-module ``follow_imports = skip``.
See :ref:`Following imports <follow-imports>` for details.

.. code-block:: python
Expand Down
10 changes: 10 additions & 0 deletions mypy/fastparse.py
Expand Up @@ -483,6 +483,16 @@ def translate_stmt_list(
and self.type_ignores
and min(self.type_ignores) < self.get_lineno(stmts[0])
):
if self.type_ignores[min(self.type_ignores)]:
self.fail(
(
"type ignore with error code is not supported for modules; "
"use `# mypy: disable-error-code=...`"
),
line=min(self.type_ignores),
column=0,
blocker=False,
)
self.errors.used_ignored_lines[self.errors.file][min(self.type_ignores)].append(
codes.FILE.code
)
Expand Down
5 changes: 5 additions & 0 deletions test-data/unit/check-errorcodes.test
Expand Up @@ -920,3 +920,8 @@ def f(d: D, s: str) -> None:
d[s] # type: ignore[literal-required]
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]


[case testRecommendErrorCode]
# type: ignore[whatever] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code=...` [syntax]
1 + "asdf"

0 comments on commit da56c97

Please sign in to comment.