(There’s previous discussion about this in mypy’s GitHub issues: Support `mypy: ignore` inline comments to suppress mypy errors · Issue #12358 · python/mypy · GitHub)
Currently, there is no way to ignore mypy-specific type errors. It’s possible to ignore typing errors for all type checkers using # type: ignore
and pyright supports ignore pyright-specific errors using # pyright: ignore
:
x: int = "" # type: ignore # works for all type checkers
assert_type(3**4, int) # pyright: ignore
# assert_type(2**4, Literal[16]) # no way to ignore this for mypy only
There have been two suggestions to solve this problem:
- Introduce a
mypy: ignore
directive. The main problem is that mypy uses the stdlibast
module to parse Python files, meaning thatast
would need to support this somehow. @NeilGirdhar suggested to add a flag to support this toast
in this thread and gh-101494. - Extend the
# type: ignore
syntax to allow type-checker specific ignores. Currently,# type: ignore
is underspecified in the Typing Specification as mypy already extends the syntax by allowing constructs like# type: ignore[foo,bar]
to ignore specific error codes. This syntax could be properly spec’ed and extended.
Personally, I like the second solution better, especially since – apart from type checker changes – it only requires a change to the Typing Spec, instead of changes to ast
for a more esoteric feature.