From f4a919aff6c296e3d084dfc4e00394c7f11415fd Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Wed, 25 Feb 2026 22:05:55 +0100 Subject: [PATCH 1/6] Allow type: ignore error codes filtering --- conformance/tests/directives_type_ignore.py | 2 +- docs/spec/directives.rst | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/conformance/tests/directives_type_ignore.py b/conformance/tests/directives_type_ignore.py index b96d23aea..f76802b94 100644 --- a/conformance/tests/directives_type_ignore.py +++ b/conformance/tests/directives_type_ignore.py @@ -11,7 +11,7 @@ y: int = "" # type: ignore - additional stuff # The following type violation should be suppressed. -z: int = "" # type: ignore[additional_stuff] +z: int = "" # type: ignore[assignment] # > In some cases, linting tools or other comments may be needed on the same # > line as a type comment. In these cases, the type comment should be before diff --git a/docs/spec/directives.rst b/docs/spec/directives.rst index 6ed7016a7..927c4081c 100644 --- a/docs/spec/directives.rst +++ b/docs/spec/directives.rst @@ -73,6 +73,13 @@ other comments and linting markers: # type: ignore # +Any text following ``# type: ignore`` is ignored, provided there is at least one +whitespace character after ``# type: ignore``. The form of +``# type: ignore[error_code1, error_code2]`` can be used to filter error codes +that may vary across type checkers. Type checkers may ignore the bracketed +codes and treat this form as equivalent to ``# type: ignore``. + + .. _`cast`: ``cast()`` From 6ce8087e86acc17f01a9f3a4035b1bd38e167a3b Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 2 Mar 2026 15:21:55 +0100 Subject: [PATCH 2/6] Try to improve the wording for the spec --- conformance/tests/directives_type_ignore.py | 4 ++++ docs/spec/directives.rst | 24 +++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/conformance/tests/directives_type_ignore.py b/conformance/tests/directives_type_ignore.py index f76802b94..f58d0d29c 100644 --- a/conformance/tests/directives_type_ignore.py +++ b/conformance/tests/directives_type_ignore.py @@ -10,6 +10,10 @@ # The following type violation should be suppressed. y: int = "" # type: ignore - additional stuff +# The following type violations should not be suppressed. +y: int = "" # type: ignored # E: Unexpected word "ignored" +y: int = "" # type: ignored, foo # E: Unexpected comma + # The following type violation should be suppressed. z: int = "" # type: ignore[assignment] diff --git a/docs/spec/directives.rst b/docs/spec/directives.rst index 927c4081c..ffed27b46 100644 --- a/docs/spec/directives.rst +++ b/docs/spec/directives.rst @@ -73,12 +73,24 @@ other comments and linting markers: # type: ignore # -Any text following ``# type: ignore`` is ignored, provided there is at least one -whitespace character after ``# type: ignore``. The form of -``# type: ignore[error_code1, error_code2]`` can be used to filter error codes -that may vary across type checkers. Type checkers may ignore the bracketed -codes and treat this form as equivalent to ``# type: ignore``. - +Text following ``# type: ignore`` must be ignored, provided there is at +least one whitespace character after it:: + + # Not valid because of the "d" after ignore; does not supress the type error + s: str = 1 # type: ignored + # Not valid because of the comma; does not supress the type error + s: str = 1 # type: ignore, because I feel like it + # This is valid because of the whitespace and must suppress the type error + s: str = 1 # type: ignore because why not + +The form of ``# type: ignore[...]`` may be used to filter errors depending on +the type checker: + +- In ``# type: ignore[my_code1]``, a type checker may ignore the error code + ``my_code1`` and choose to treat this form as equivalent to ``# type: ignore``. +- Alternatively in ``# type: ignore[my_code1]`` a type checker may suppress the + error only if the error cause matches the error code ``my_code1``. +- ``# type: ignore`` must always suppress all errors. .. _`cast`: From 5cbcac430b07dcf87c07d54245051786116192de Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 2 Mar 2026 16:42:39 +0100 Subject: [PATCH 3/6] Rephrase a bit of the spec --- docs/spec/directives.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/spec/directives.rst b/docs/spec/directives.rst index ffed27b46..bc6c97e86 100644 --- a/docs/spec/directives.rst +++ b/docs/spec/directives.rst @@ -73,8 +73,8 @@ other comments and linting markers: # type: ignore # -Text following ``# type: ignore`` must be ignored, provided there is at -least one whitespace character after it:: +Text following ``# type: ignore - some text`` is equivalent to +``type: ignore``, provided there is at least one whitespace character after it:: # Not valid because of the "d" after ignore; does not supress the type error s: str = 1 # type: ignored From ffc8e7c9585d3ebffe304e5257b5e8fdea632509 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Tue, 14 Apr 2026 02:37:03 +0200 Subject: [PATCH 4/6] Avoid specifying how whitespace after # type: ignore works --- conformance/results/mypy/directives_type_ignore.toml | 3 --- conformance/results/results.html | 2 +- conformance/results/zuban/directives_type_ignore.toml | 9 +-------- conformance/tests/directives_type_ignore.py | 4 ---- docs/spec/directives.rst | 10 ---------- 5 files changed, 2 insertions(+), 26 deletions(-) diff --git a/conformance/results/mypy/directives_type_ignore.toml b/conformance/results/mypy/directives_type_ignore.toml index 8dfefb377..5ac164ed4 100644 --- a/conformance/results/mypy/directives_type_ignore.toml +++ b/conformance/results/mypy/directives_type_ignore.toml @@ -5,11 +5,8 @@ Does not honor "# type: ignore" comment if comment includes additional text. output = """ directives_type_ignore.py:11: error: Invalid "type: ignore" comment [syntax] directives_type_ignore.py:11: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment] -directives_type_ignore.py:14: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment] -directives_type_ignore.py:14: note: Error code "assignment" not covered by "type: ignore" comment """ conformance_automated = "Fail" errors_diff = """ Line 11: Unexpected errors ['directives_type_ignore.py:11: error: Invalid "type: ignore" comment [syntax]', 'directives_type_ignore.py:11: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]'] -Line 14: Unexpected errors ['directives_type_ignore.py:14: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]'] """ diff --git a/conformance/results/results.html b/conformance/results/results.html index 95f5422b5..1528d66e0 100644 --- a/conformance/results/results.html +++ b/conformance/results/results.html @@ -1191,7 +1191,7 @@

Python Type System Conformance Test Results

     directives_type_ignore
Partial

Does not honor "# type: ignore" comment if comment includes additional text.

Pass -
Partial

Does not honor "# type: ignore" comment if comment includes additional text.

+Pass Pass
Partial

Treats `# type: ignore[error-code]` as only ignoring errors that match the error code `error-code`.

diff --git a/conformance/results/zuban/directives_type_ignore.toml b/conformance/results/zuban/directives_type_ignore.toml index 2e7de8395..cdd4d0cd9 100644 --- a/conformance/results/zuban/directives_type_ignore.toml +++ b/conformance/results/zuban/directives_type_ignore.toml @@ -1,12 +1,5 @@ -conformant = "Partial" -notes = """ -Does not honor "# type: ignore" comment if comment includes additional text. -""" -conformance_automated = "Fail" +conformance_automated = "Pass" errors_diff = """ -Line 14: Unexpected errors ['directives_type_ignore.py:14: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]'] """ output = """ -directives_type_ignore.py:14: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment] -directives_type_ignore.py:14: note: Error code "assignment" not covered by "type: ignore" comment """ diff --git a/conformance/tests/directives_type_ignore.py b/conformance/tests/directives_type_ignore.py index f58d0d29c..f76802b94 100644 --- a/conformance/tests/directives_type_ignore.py +++ b/conformance/tests/directives_type_ignore.py @@ -10,10 +10,6 @@ # The following type violation should be suppressed. y: int = "" # type: ignore - additional stuff -# The following type violations should not be suppressed. -y: int = "" # type: ignored # E: Unexpected word "ignored" -y: int = "" # type: ignored, foo # E: Unexpected comma - # The following type violation should be suppressed. z: int = "" # type: ignore[assignment] diff --git a/docs/spec/directives.rst b/docs/spec/directives.rst index bc6c97e86..68dc9f31c 100644 --- a/docs/spec/directives.rst +++ b/docs/spec/directives.rst @@ -73,16 +73,6 @@ other comments and linting markers: # type: ignore # -Text following ``# type: ignore - some text`` is equivalent to -``type: ignore``, provided there is at least one whitespace character after it:: - - # Not valid because of the "d" after ignore; does not supress the type error - s: str = 1 # type: ignored - # Not valid because of the comma; does not supress the type error - s: str = 1 # type: ignore, because I feel like it - # This is valid because of the whitespace and must suppress the type error - s: str = 1 # type: ignore because why not - The form of ``# type: ignore[...]`` may be used to filter errors depending on the type checker: From a0bc8ad05ce72abfd10c2b3a685a55340ad30a2a Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Wed, 15 Apr 2026 11:26:03 +0000 Subject: [PATCH 5/6] Update conformance/tests/directives_type_ignore.py Co-authored-by: Carl Meyer --- conformance/tests/directives_type_ignore.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/conformance/tests/directives_type_ignore.py b/conformance/tests/directives_type_ignore.py index f76802b94..ef44c353f 100644 --- a/conformance/tests/directives_type_ignore.py +++ b/conformance/tests/directives_type_ignore.py @@ -10,7 +10,9 @@ # The following type violation should be suppressed. y: int = "" # type: ignore - additional stuff -# The following type violation should be suppressed. +# The following type violation may be suppressed, depending whether the +# type checker uses the error code `assignment` for invalid assignments, +# and/or how it treats unknown error codes. z: int = "" # type: ignore[assignment] # > In some cases, linting tools or other comments may be needed on the same From 614bf6006a23c49b527634850d42ac0d2266f0de Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Wed, 15 Apr 2026 11:29:29 +0000 Subject: [PATCH 6/6] Update docs/spec/directives.rst Co-authored-by: Carl Meyer --- docs/spec/directives.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/spec/directives.rst b/docs/spec/directives.rst index 68dc9f31c..5a7d5e7cb 100644 --- a/docs/spec/directives.rst +++ b/docs/spec/directives.rst @@ -73,14 +73,14 @@ other comments and linting markers: # type: ignore # -The form of ``# type: ignore[...]`` may be used to filter errors depending on -the type checker: +The form ``# type: ignore[...]`` may be used to suppress only type errors with a +given error code, though support for this is optional and may vary by type checker: -- In ``# type: ignore[my_code1]``, a type checker may ignore the error code +- Given ``# type: ignore[my_code1]``, a type checker may ignore the error code ``my_code1`` and choose to treat this form as equivalent to ``# type: ignore``. -- Alternatively in ``# type: ignore[my_code1]`` a type checker may suppress the +- Alternatively, given ``# type: ignore[my_code1]`` a type checker may suppress the error only if the error cause matches the error code ``my_code1``. -- ``# type: ignore`` must always suppress all errors. +- A bare ``# type: ignore`` must always suppress all type errors. .. _`cast`: