Skip to content
6 changes: 5 additions & 1 deletion conformance/tests/directives_type_ignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
# 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.
Comment thread
davidhalter marked this conversation as resolved.
Outdated
z: int = "" # type: ignore[additional_stuff]
z: int = "" # type: ignore[assignment]
Comment thread
carljm marked this conversation as resolved.
Outdated

# > 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
Expand Down
19 changes: 19 additions & 0 deletions docs/spec/directives.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ other comments and linting markers:

# type: ignore # <comment or other marker>

Text following ``# type: ignore`` must be ignored, provided there is at
Comment thread
davidhalter marked this conversation as resolved.
Outdated
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
Comment thread
davidhalter marked this conversation as resolved.
Outdated
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.
Comment thread
davidhalter marked this conversation as resolved.
Outdated

.. _`cast`:

``cast()``
Expand Down
Loading