Skip to content

Deduplicate error codes for ignore-without-code #12194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]
-------------------------------------------------------------------------

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/source/error_codes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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<ignore-without-code>` for more information.


.. _silence-error-codes:

Silencing errors based on error codes
Expand Down
4 changes: 2 additions & 2 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
14 changes: 13 additions & 1 deletion test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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 ]
Expand Down