From f4a919aff6c296e3d084dfc4e00394c7f11415fd Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Wed, 25 Feb 2026 22:05:55 +0100 Subject: [PATCH 1/3] 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/3] 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/3] 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