From b310b1874288e8db25e2c1671f7afde0e631d1ca Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 17 Feb 2022 01:25:31 +0100 Subject: [PATCH 1/4] Remove duplicate error codes from message --- mypy/errors.py | 2 +- test-data/unit/check-errorcodes.test | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/mypy/errors.py b/mypy/errors.py index 9744f134ac166..6c27b34547d64 100644 --- a/mypy/errors.py +++ b/mypy/errors.py @@ -532,7 +532,7 @@ def generate_ignore_without_code_errors(self, continue codes_hint = '' - ignored_codes = used_ignored_lines[line] + ignored_codes: List[str] = sorted(set(used_ignored_lines[line])) if ignored_codes: codes_hint = f' (currently ignored: [{", ".join(ignored_codes)}])' diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index 642828085b536..5e3b120a82a6c 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -164,6 +164,18 @@ z # type: ignore[ignore-without-code] # E: Unused "type: ignore" comment # E: Na x y # type: ignore # ignore the lack of error code since we're ignore the whole file +[case testErrorCodeMissingMultiple] +# flags: --enable-error-code ignore-without-code --python-version 3.7 +from __future__ import annotations +class A: + attr: int + def func(self, var: int) -> A | None: ... + +a: A | None +# 'union-attr' should only be listed once (instead of twice) and list should be sorted +a.func("invalid string").attr # type: ignore # E: "type: ignore" comment without error code (currently ignored: [arg-type, union-attr]) [ignore-without-code] +[builtins fixtures/tuple.pyi] + [case testErrorCodeIgnoreWithExtraSpace] x # type: ignore [name-defined] x2 # type: ignore [ name-defined ] From e57f0d2513f865db96c352042826aafb323d8393 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 17 Feb 2022 01:37:57 +0100 Subject: [PATCH 2/4] Crossref ignore-without-code --- docs/source/error_code_list2.rst | 2 ++ docs/source/error_codes.rst | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/docs/source/error_code_list2.rst b/docs/source/error_code_list2.rst index ba5b67a5aeb78..ff366e1f110dd 100644 --- a/docs/source/error_code_list2.rst +++ b/docs/source/error_code_list2.rst @@ -257,6 +257,8 @@ except that attempting to invoke an undefined method (e.g. ``__len__``) results while attempting to evaluate an object in boolean context without a concrete implementation results in a truthy value. +.. _ignore-without-code: + Check that ``# type: ignore`` include an error code [ignore-without-code] ------------------------------------------------------------------------- diff --git a/docs/source/error_codes.rst b/docs/source/error_codes.rst index 5255a6984b7b9..08d56c59fba20 100644 --- a/docs/source/error_codes.rst +++ b/docs/source/error_codes.rst @@ -31,6 +31,10 @@ or config `show_error_codes = True` to display error codes. Error codes are show $ mypy --show-error-codes prog.py prog.py:1: error: "str" has no attribute "trim" [attr-defined] +It's also possible to require error codes for ``type: ignore`` comments. +See :ref:`ignore-without-code` for more information. + + .. _silence-error-codes: Silencing errors based on error codes From 2549b48571469bcaec8686ec59389ce9bd643501 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Wed, 16 Feb 2022 17:21:46 -0800 Subject: [PATCH 3/4] Update mypy/errors.py --- mypy/errors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/errors.py b/mypy/errors.py index 6c27b34547d64..c6d2ad8d95313 100644 --- a/mypy/errors.py +++ b/mypy/errors.py @@ -532,7 +532,7 @@ def generate_ignore_without_code_errors(self, continue codes_hint = '' - ignored_codes: List[str] = sorted(set(used_ignored_lines[line])) + ignored_codes = sorted(set(used_ignored_lines[line])) if ignored_codes: codes_hint = f' (currently ignored: [{", ".join(ignored_codes)}])' From 02aa0c3a4faa3f742c6d53a8784442e2e3f7f234 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 17 Feb 2022 08:57:32 +0100 Subject: [PATCH 4/4] Update error message --- docs/source/error_code_list2.rst | 2 +- mypy/errors.py | 2 +- test-data/unit/check-errorcodes.test | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/source/error_code_list2.rst b/docs/source/error_code_list2.rst index ff366e1f110dd..f9c9a64071d17 100644 --- a/docs/source/error_code_list2.rst +++ b/docs/source/error_code_list2.rst @@ -282,7 +282,7 @@ Example: # - the expected error 'assignment', and # - the unexpected error 'attr-defined' # are silenced. - # Error: "type: ignore" comment without error code (currently ignored: [attr-defined]) + # Error: "type: ignore" comment without error code (use "type: ignore[attr-defined]" instead) f.nme = 42 # type: ignore # This line warns correctly about the typo in the attribute name diff --git a/mypy/errors.py b/mypy/errors.py index c6d2ad8d95313..62e82048e964b 100644 --- a/mypy/errors.py +++ b/mypy/errors.py @@ -534,7 +534,7 @@ def generate_ignore_without_code_errors(self, codes_hint = '' ignored_codes = sorted(set(used_ignored_lines[line])) if ignored_codes: - codes_hint = f' (currently ignored: [{", ".join(ignored_codes)}])' + codes_hint = f' (use "type: ignore[{", ".join(ignored_codes)}]" instead)' message = f'"type: ignore" comment without error code{codes_hint}' # Don't use report since add_error_info will ignore the error! diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index 5e3b120a82a6c..3c85ae48bb29a 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -148,7 +148,7 @@ x # type: ignore[name-defined, attr-defined] # E: Unused "type: ignore[attr-defi [case testErrorCodeMissingWhenRequired] # flags: --enable-error-code ignore-without-code "x" # type: ignore # E: "type: ignore" comment without error code [ignore-without-code] -y # type: ignore # E: "type: ignore" comment without error code (currently ignored: [name-defined]) [ignore-without-code] +y # type: ignore # E: "type: ignore" comment without error code (use "type: ignore[name-defined]" instead) [ignore-without-code] z # type: ignore[name-defined] "a" # type: ignore[ignore-without-code] @@ -173,7 +173,7 @@ class A: a: A | None # 'union-attr' should only be listed once (instead of twice) and list should be sorted -a.func("invalid string").attr # type: ignore # E: "type: ignore" comment without error code (currently ignored: [arg-type, union-attr]) [ignore-without-code] +a.func("invalid string").attr # type: ignore # E: "type: ignore" comment without error code (use "type: ignore[arg-type, union-attr]" instead) [ignore-without-code] [builtins fixtures/tuple.pyi] [case testErrorCodeIgnoreWithExtraSpace]