Skip to content

Warn on module level type ignore with error code #13512

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

Merged
merged 4 commits into from
Aug 26, 2022
Merged
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: 7 additions & 1 deletion docs/source/common_issues.rst
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 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
Original file line number Diff line number Diff line change
Expand Up @@ -916,3 +916,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"