From 9dd525df131a50ef7e843048437c7f532948f3f4 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 24 Aug 2022 20:32:29 -0700 Subject: [PATCH 1/4] Warn on module level type ignore with error code 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 --- docs/source/common_issues.rst | 6 +++++- mypy/fastparse.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index d2302469518d..f457c3df7b23 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -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 diff --git a/mypy/fastparse.py b/mypy/fastparse.py index ad7bf2ddf06b..40adb165a746 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -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 ) From 716ed0fdeac1a417819a188cea253e6b22af2316 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 25 Aug 2022 10:25:30 -0700 Subject: [PATCH 2/4] add a test --- test-data/unit/check-errorcodes.test | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index f1a6f3c77ada..498f7ea9862c 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -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 at module level; use `# mypy: disable-error-code=...` [syntax] +1 + "asdf" From 2d846086d3a6c06f089f9135883acbbae80b7efc Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 25 Aug 2022 10:26:59 -0700 Subject: [PATCH 3/4] disambiguate error message --- mypy/fastparse.py | 2 +- test-data/unit/check-errorcodes.test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 40adb165a746..877f822fb3d9 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -485,7 +485,7 @@ def translate_stmt_list( if self.type_ignores[min(self.type_ignores)]: self.fail( ( - "type ignore with error code is not supported at module level; " + "type ignore with error code is not supported for modules; " "use `# mypy: disable-error-code=...`" ), line=min(self.type_ignores), diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index 498f7ea9862c..b3acbddb65c9 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -919,5 +919,5 @@ def f(d: D, s: str) -> None: [case testRecommendErrorCode] -# type: ignore[whatever] # E: type ignore with error code is not supported at module level; use `# mypy: disable-error-code=...` [syntax] +# type: ignore[whatever] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code=...` [syntax] 1 + "asdf" From ded208b5bd3245a528147e45796bff051ffc1a20 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 25 Aug 2022 10:36:24 -0700 Subject: [PATCH 4/4] also mention follow_imports=skip --- docs/source/common_issues.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index f457c3df7b23..42962581702f 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -192,6 +192,8 @@ including imports or docstrings) has the effect of ignoring the entire contents 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