diff --git a/docs/source/error_code_list2.rst b/docs/source/error_code_list2.rst index ba5b67a5aeb78..f9c9a64071d17 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] ------------------------------------------------------------------------- @@ -280,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/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 diff --git a/mypy/errors.py b/mypy/errors.py index 9744f134ac166..62e82048e964b 100644 --- a/mypy/errors.py +++ b/mypy/errors.py @@ -532,9 +532,9 @@ def generate_ignore_without_code_errors(self, continue codes_hint = '' - ignored_codes = used_ignored_lines[line] + 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 642828085b536..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] @@ -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 (use "type: ignore[arg-type, union-attr]" instead) [ignore-without-code] +[builtins fixtures/tuple.pyi] + [case testErrorCodeIgnoreWithExtraSpace] x # type: ignore [name-defined] x2 # type: ignore [ name-defined ]