From c80d73643a17320ca05deb6fb82c8bccf5779bb7 Mon Sep 17 00:00:00 2001 From: codenamenam Date: Sun, 12 Oct 2025 16:02:03 +0900 Subject: [PATCH 1/2] fix: Ignore empty error codes from 'type: ignore' --- mypy/fastparse.py | 2 +- test-data/unit/check-errorcodes.test | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 276e183a6bf0..0c07d4379ce9 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 [code.strip() for code in m.group(1).split(",") if 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] From f0013203e4ddadccc0baf758a92e6359c47a1f6c Mon Sep 17 00:00:00 2001 From: codenamenam Date: Mon, 13 Oct 2025 09:44:28 +0900 Subject: [PATCH 2/2] fix: Use walrus operator --- mypy/fastparse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 0c07d4379ce9..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(",") if code.strip()] + return [stripped_code for code in m.group(1).split(",") if (stripped_code := code.strip())] def parse_type_comment(