Skip to content

Commit 5b4ffa9

Browse files
authored
Deduplicate error codes for ignore-without-code (#12194)
1 parent 1de5e55 commit 5b4ffa9

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

docs/source/error_code_list2.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ except that attempting to invoke an undefined method (e.g. ``__len__``) results
257257
while attempting to evaluate an object in boolean context without a concrete implementation results in a truthy value.
258258

259259

260+
.. _ignore-without-code:
261+
260262
Check that ``# type: ignore`` include an error code [ignore-without-code]
261263
-------------------------------------------------------------------------
262264

@@ -280,7 +282,7 @@ Example:
280282
# - the expected error 'assignment', and
281283
# - the unexpected error 'attr-defined'
282284
# are silenced.
283-
# Error: "type: ignore" comment without error code (currently ignored: [attr-defined])
285+
# Error: "type: ignore" comment without error code (use "type: ignore[attr-defined]" instead)
284286
f.nme = 42 # type: ignore
285287
286288
# This line warns correctly about the typo in the attribute name

docs/source/error_codes.rst

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ or config `show_error_codes = True` to display error codes. Error codes are show
3131
$ mypy --show-error-codes prog.py
3232
prog.py:1: error: "str" has no attribute "trim" [attr-defined]
3333
34+
It's also possible to require error codes for ``type: ignore`` comments.
35+
See :ref:`ignore-without-code<ignore-without-code>` for more information.
36+
37+
3438
.. _silence-error-codes:
3539

3640
Silencing errors based on error codes

mypy/errors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,9 @@ def generate_ignore_without_code_errors(self,
532532
continue
533533

534534
codes_hint = ''
535-
ignored_codes = used_ignored_lines[line]
535+
ignored_codes = sorted(set(used_ignored_lines[line]))
536536
if ignored_codes:
537-
codes_hint = f' (currently ignored: [{", ".join(ignored_codes)}])'
537+
codes_hint = f' (use "type: ignore[{", ".join(ignored_codes)}]" instead)'
538538

539539
message = f'"type: ignore" comment without error code{codes_hint}'
540540
# Don't use report since add_error_info will ignore the error!

test-data/unit/check-errorcodes.test

+13-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ x # type: ignore[name-defined, attr-defined] # E: Unused "type: ignore[attr-defi
148148
[case testErrorCodeMissingWhenRequired]
149149
# flags: --enable-error-code ignore-without-code
150150
"x" # type: ignore # E: "type: ignore" comment without error code [ignore-without-code]
151-
y # type: ignore # E: "type: ignore" comment without error code (currently ignored: [name-defined]) [ignore-without-code]
151+
y # type: ignore # E: "type: ignore" comment without error code (use "type: ignore[name-defined]" instead) [ignore-without-code]
152152
z # type: ignore[name-defined]
153153
"a" # type: ignore[ignore-without-code]
154154

@@ -164,6 +164,18 @@ z # type: ignore[ignore-without-code] # E: Unused "type: ignore" comment # E: Na
164164
x
165165
y # type: ignore # ignore the lack of error code since we're ignore the whole file
166166

167+
[case testErrorCodeMissingMultiple]
168+
# flags: --enable-error-code ignore-without-code --python-version 3.7
169+
from __future__ import annotations
170+
class A:
171+
attr: int
172+
def func(self, var: int) -> A | None: ...
173+
174+
a: A | None
175+
# 'union-attr' should only be listed once (instead of twice) and list should be sorted
176+
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]
177+
[builtins fixtures/tuple.pyi]
178+
167179
[case testErrorCodeIgnoreWithExtraSpace]
168180
x # type: ignore [name-defined]
169181
x2 # type: ignore [ name-defined ]

0 commit comments

Comments
 (0)