Skip to content
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
9 changes: 6 additions & 3 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,13 @@ def add_error_info(self, info: ErrorInfo) -> None:
# line == end_line for most nodes, so we only loop once.
for scope_line in lines:
if self.is_ignored_error(scope_line, info, self.ignored_lines[file]):
err_code = info.code or codes.MISC
if not self.is_error_code_enabled(err_code):
# Error code is disabled - don't mark the current
# "type: ignore" comment as used.
return
# Annotation requests us to ignore all errors on this line.
self.used_ignored_lines[file][scope_line].append(
(info.code or codes.MISC).code
)
self.used_ignored_lines[file][scope_line].append(err_code.code)
return
if file in self.ignored_files:
return
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ x # type: ignore[name-defined, attr-defined] # E: Unused "type: ignore[attr-defi
# flags: --warn-unused-ignores
"x" # type: ignore[name-defined] # E: Unused "type: ignore" comment [unused-ignore]

[case testErrorCodeWarnUnusedIgnores7_WarnWhenErrorCodeDisabled]
# flags: --warn-unused-ignores --disable-error-code name-defined
x # type: ignore # E: Unused "type: ignore" comment [unused-ignore]
x # type: ignore[name-defined] # E: Unused "type: ignore" comment [unused-ignore]
"x".foobar(y) # type: ignore[name-defined, attr-defined] # E: Unused "type: ignore[name-defined]" comment [unused-ignore]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe also check interactions with # type: ignore[unused-ignore]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some tests! The behavior seems to be what we'd want - adding # type: ignore[unused-ignore] suppresses any unused-ignore warnings. There's no way to make mypy warn about an unused # type: ignore[unused-ignore], but I don't think that's a problem?


[case testErrorCodeWarnUnusedIgnores8_IgnoreUnusedIgnore]
# flags: --warn-unused-ignores --disable-error-code name-defined
"x" # type: ignore[unused-ignore]
"x" # type: ignore[name-defined, unused-ignore]
"x" # type: ignore[xyz, unused-ignore]
x # type: ignore[name-defined, unused-ignore]

[case testErrorCodeMissingWhenRequired]
# flags: --enable-error-code ignore-without-code
"x" # type: ignore # E: "type: ignore" comment without error code [ignore-without-code]
Expand Down