diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index d2302469518d..42962581702f 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -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 ` for details. .. code-block:: python diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 0c80803d84d3..ff3a15d4d33b 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -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 ) diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index 640c793f99a2..a599a6e75418 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -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"