Skip to content

Commit

Permalink
Warn on module level type ignore with error code
Browse files Browse the repository at this point in the history
Per-module error codes were added in python#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 python#13435, fixes python#12076, fixes python#11999, fixes python#11027, fixes python#9318,
fixes python#7839
  • Loading branch information
hauntsaninja committed Aug 25, 2022
1 parent 3d015af commit 9dd525d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion docs/source/common_issues.rst
Expand Up @@ -187,7 +187,11 @@ 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.

.. code-block:: python
Expand Down
10 changes: 10 additions & 0 deletions mypy/fastparse.py
Expand Up @@ -482,6 +482,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 at module level; "
"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

0 comments on commit 9dd525d

Please sign in to comment.