Skip to content

Commit c80d736

Browse files
committed
fix: Ignore empty error codes from 'type: ignore'
1 parent 6aa44da commit c80d736

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

mypy/fastparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def parse_type_ignore_tag(tag: str | None) -> list[str] | None:
305305
if m is None:
306306
# Invalid "# type: ignore" comment.
307307
return None
308-
return [code.strip() for code in m.group(1).split(",")]
308+
return [code.strip() for code in m.group(1).split(",") if code.strip()]
309309

310310

311311
def parse_type_comment(

test-data/unit/check-errorcodes.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ x # type: ignore[name-defined] # E: Unused "type: ig
118118
"x" # type: ignore[xyz, unused-ignore]
119119
x # type: ignore[name-defined, unused-ignore]
120120

121+
[case testErrorCodeWarnUnusedIgnores9_SloppyFormattingNotEmptyBrackets]
122+
# flags: --warn-unused-ignores
123+
1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined]
124+
1 + "ok" + "ok".foo # type: ignore[ ,operator,attr-defined]
125+
1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined,]
126+
1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined,,]
127+
128+
[case testErrorCodeWarnUnusedIgnores10_SloppyFormattingEmptyBrackets]
129+
# flags: --warn-unused-ignores
130+
1+1 # type: ignore # E: Unused "type: ignore" comment [unused-ignore]
131+
1+1 # type: ignore[] # E: Unused "type: ignore" comment [unused-ignore]
132+
1+1 # type: ignore[ ] # E: Unused "type: ignore" comment [unused-ignore]
133+
1+1 # type: ignore[,,, ] # E: Unused "type: ignore" comment [unused-ignore]
134+
121135
[case testErrorCodeMissingWhenRequired]
122136
# flags: --enable-error-code ignore-without-code
123137
"x" # type: ignore # E: "type: ignore" comment without error code [ignore-without-code]

0 commit comments

Comments
 (0)