diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 276e183a6bf0..ca5c483223de 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -305,7 +305,7 @@ def parse_type_ignore_tag(tag: str | None) -> list[str] | None: if m is None: # Invalid "# type: ignore" comment. return None - return [code.strip() for code in m.group(1).split(",")] + return [stripped_code for code in m.group(1).split(",") if (stripped_code := code.strip())] def parse_type_comment( diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index 06c5753db5a7..b1009f8fe085 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -118,6 +118,20 @@ x # type: ignore[name-defined] # E: Unused "type: ig "x" # type: ignore[xyz, unused-ignore] x # type: ignore[name-defined, unused-ignore] +[case testErrorCodeWarnUnusedIgnores9_SloppyFormattingNotEmptyBrackets] +# flags: --warn-unused-ignores +1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined] +1 + "ok" + "ok".foo # type: ignore[ ,operator,attr-defined] +1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined,] +1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined,,] + +[case testErrorCodeWarnUnusedIgnores10_SloppyFormattingEmptyBrackets] +# flags: --warn-unused-ignores +1+1 # type: ignore # E: Unused "type: ignore" comment [unused-ignore] +1+1 # type: ignore[] # E: Unused "type: ignore" comment [unused-ignore] +1+1 # type: ignore[ ] # E: Unused "type: ignore" comment [unused-ignore] +1+1 # type: ignore[,,, ] # E: Unused "type: ignore" comment [unused-ignore] + [case testErrorCodeMissingWhenRequired] # flags: --enable-error-code ignore-without-code "x" # type: ignore # E: "type: ignore" comment without error code [ignore-without-code]