You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider best-effort supporting mypy error code names for some common cases #3127
diagnosticsRelated to reporting of diagnostics.needs-designNeeds further design before implementationsuppressionRelated to supression of violations e.g. ty:ignore
For pyca/cryptography, we'd like our code to type check under both mypy and ty. Currently one of the largest impediments is that we have a bunch of tests that cover the TypeError cases of various APIs, and so thsoe lines have # type: ignore comments.
However, mypy and ty have different names for the ignored errors, e.g.:
error[invalid-argument-type]: Argument to bound method `__init__` is incorrect
--> tests/hazmat/primitives/decrepit/test_algorithms.py:114:22
|
112 | def test_invalid_key_type(self):
113 | with pytest.raises(TypeError, match="key must be bytes"):
114 | Blowfish("0" * 8) # type: ignore[arg-type]
| ^^^^^^^ Expected `bytes`, found `Literal["00000000"]`
It's not clear to me what the desired pattern/style for attempting to do this is.
Version
No response
Reacted by Anatoli Babenia, Alessio, Luco Bellic and Maciej Olko
We just shipped 0.0.25 with a new approach for this. We now support # type: ignore[ty:invalid-argument-type] as equivalent to # ty: ignore[invalid-argument-type], and we support multiple codes within the brackets, so if you are supporting both mypy and ty, you can use # type: ignore[arg-type, ty:invalid-argument-type]. Any codes not prefixed by ty: we will assume belong to a different type checker and we will ignore. (Previously we silently treated the unknown codes as blanket suppressions, so you may be seeing these as new errors in 0.0.25).
This is still a bit verbose, but it's better than # type: ignore[arg-type] # ty: ignore[invalid-argument-type], which was the previous best option.
Agreed, that putting them both in a single ignore is a step forward.
It does feel like for a common case like invalid-argument-type, it'd be much more ergonomic to accept the mypy spelling. There's some cross-compataibility risk there, but I think it's a smoother onboarding experience. WDYT?
We could consider this. It feels risky/infeasible to try to do it consistently across the board, since different type checkers may slice the error space differently, so there isn't necessarily 1:1 alignment between error codes. And there's some risk of confusion if we do it for some codes and not for others. But I agree that for some codes, the alignment is probably pretty good.
changed the title [-]ignore names vary between ty and mypy: What's the right approach for writing code that type checks under either?[/-][+]Consider best-effort supporting mypy error code names for some common cases[/+]on Mar 25, 2026
We could consider this. It feels risky/infeasible to try to do it consistently across the board, since different type checkers may slice the error space differently, so there isn't necessarily 1:1 alignment between error codes. And there's some risk of confusion if we do it for some codes and not for others. But I agree that for some codes, the alignment is probably pretty good.
I'm not sure how much this would realistically help, given that we currently require error codes to be ty:-prefixed in type: ignore comments. So if we changed invalid-argument-type to arg-type, you'd still have to change your type: ignore comment from type: ignore[arg-type] to either type: ignore[arg-type, ty:arg-type] or (if you're no longer using mypy on your codebase) type: ignore[ty: arg-type].
In Ruff, we've also historically tried to abide by our documented naming convention, and so far I think our ty rules have also generally held to this naming convention. Mypy error codes such as arg-type break from this convention.
As @carljm says, there are also lots of cases where our categories do not line up 1:1 with mypy's.
And what about cases where pyright and mypy have different error codes for the same thing? (I think this is almost always the case, since pyright prefers reportCamelCase names.) Do we just always prefer the mypy error code here, ignoring the fact that many users use pyright? (I guess the answer is "yes", since pyright only recognises specific error codes if you use pyright: ignore, never type: ignore, so it practically wouldn't be very useful to anybody to copy pyright's error codes. It still seems weird to privilege mypy uses over pyright users, though.)
Oh, I guess the concrete suggestion here is just that we'd recognise the mypy type: ignore comment rather than actually renaming our error codes to match mypy? I still don't love that idea -- it feels like it would be weirdly inconsistent to recognise some mypy ignore comments but not others. If we want to reduce the onboarding hurdle, I'd prefer to ship a tool that helps users codemod their ignore comments where possible
different type checkers may slice the error space differently, so there isn't necessarily 1:1 alignment between error codes. And there's some risk of confusion if we do it for some codes and not for others
Push the exact mapping to the user side? I like that ty errors are more specific - like invalid-return-type is less vague/more readable/specific than return-value. The mapping would look like this:
FWIW, another reason this would be helpful is that if you run mypy with warn-unused-ignores, then # type: ignore[mypy-code, ty:ty-code] produces an unused ignore warning on mypy (for the ty:ty-code ignore).
I'm not sure if there's any effort to standardize the tool:rule syntax such that mypy automatically ignores ty: as "not for me"?
Mypy have said in the past that they consider that to be a mypy bug (or at least a missing feature); part of the reason why we switched our supported type: ignore patterns was that mypy said this was their preferred pattern for folks who run multiple type checkers on their codebase
Their preferred pattern is a thing they don't support? I guess generously this is a "this is the right way to evolve the ecosystem" thing. I shall move my confusion to their issue tracker, thanks much.
I'd be supportive of this, depending on the complexity it introduces. There are a few cases worth calling out:
This is a pre-existing issue that we should fix, but i think it becomes worse with this feature. A ty:ignore comment should always take precedence over a type:ignore comment (with or without code). This is unlike today, where ty always uses the closest suppression comment
--add-ignore should not append to a type:ignore that only uses mypy error codes (it's okay if it contains at least one type:ignore[ty:code])
The unused ignore comment diagnostic should show the mypy error code if it's a mypy suppression
Only support rules with 1:1 mapping
How can users opt out of this behavior but still want to use type:ignore. E.g. so that it's explicit which type checker requires which suppressions?
That sounds nice, but is probably more complicated to implement because now you need to support that fully in --add-ignore. We might also need to consider whether this replaces respect-type-ignores to avoid that the two options contradict each other.
I think the conclusion internally is that this seems desirable but it needs design.
diagnosticsRelated to reporting of diagnostics.needs-designNeeds further design before implementationsuppressionRelated to supression of violations e.g. ty:ignore
Activity
carljm commentedon Mar 24, 2026
We just shipped 0.0.25 with a new approach for this. We now support
# type: ignore[ty:invalid-argument-type]as equivalent to# ty: ignore[invalid-argument-type], and we support multiple codes within the brackets, so if you are supporting both mypy and ty, you can use# type: ignore[arg-type, ty:invalid-argument-type]. Any codes not prefixed byty:we will assume belong to a different type checker and we will ignore. (Previously we silently treated the unknown codes as blanket suppressions, so you may be seeing these as new errors in 0.0.25).This is still a bit verbose, but it's better than
# type: ignore[arg-type] # ty: ignore[invalid-argument-type], which was the previous best option.alex commentedon Mar 24, 2026
Agreed, that putting them both in a single ignore is a step forward.
It does feel like for a common case like invalid-argument-type, it'd be much more ergonomic to accept the mypy spelling. There's some cross-compataibility risk there, but I think it's a smoother onboarding experience. WDYT?
carljm commentedon Mar 25, 2026
We could consider this. It feels risky/infeasible to try to do it consistently across the board, since different type checkers may slice the error space differently, so there isn't necessarily 1:1 alignment between error codes. And there's some risk of confusion if we do it for some codes and not for others. But I agree that for some codes, the alignment is probably pretty good.
Interested in what @MichaReiser and @AlexWaygood think.
alex commentedon Mar 25, 2026
FWIW, an example of a place where I wouldn't necessarily recommend trying to emulate is something like:
Right now mypy gives you a
list-itemerror and ty gives you an arg-type :-)carljm commentedon Mar 25, 2026
Yeah, that's a pretty good example of why this code-matching would be best-effort at best.
alex commentedon Mar 25, 2026
Yeah. As a practical matter I think
invalid-argument-type=arg-typecaptures like 90% of the value (for us it's 376/400 ty findings).[-]ignore names vary between ty and mypy: What's the right approach for writing code that type checks under either?[/-][+]Consider best-effort supporting mypy error code names for some common cases[/+]carljm commentedon Mar 25, 2026
Another user reports that an equivalence between mypy's
attr-definedand ourunresolved-attributewould help them similarly.AlexWaygood commentedon Mar 25, 2026
I'm not sure how much this would realistically help, given that we currently require error codes to be
ty:-prefixed intype: ignorecomments. So if we changedinvalid-argument-typetoarg-type, you'd still have to change yourtype: ignorecomment fromtype: ignore[arg-type]to eithertype: ignore[arg-type, ty:arg-type]or (if you're no longer using mypy on your codebase)type: ignore[ty: arg-type].In Ruff, we've also historically tried to abide by our documented naming convention, and so far I think our ty rules have also generally held to this naming convention. Mypy error codes such as
arg-typebreak from this convention.As @carljm says, there are also lots of cases where our categories do not line up 1:1 with mypy's.
And what about cases where pyright and mypy have different error codes for the same thing? (I think this is almost always the case, since pyright prefers
reportCamelCasenames.) Do we just always prefer the mypy error code here, ignoring the fact that many users use pyright? (I guess the answer is "yes", since pyright only recognises specific error codes if you usepyright: ignore, nevertype: ignore, so it practically wouldn't be very useful to anybody to copy pyright's error codes. It still seems weird to privilege mypy uses over pyright users, though.)AlexWaygood commentedon Mar 25, 2026
Oh, I guess the concrete suggestion here is just that we'd recognise the mypy
type: ignorecomment rather than actually renaming our error codes to match mypy? I still don't love that idea -- it feels like it would be weirdly inconsistent to recognise some mypy ignore comments but not others. If we want to reduce the onboarding hurdle, I'd prefer to ship a tool that helps users codemod their ignore comments where possibleMichaReiser commentedon Mar 25, 2026
I'm not opposed to it, but there are certainly some wrinkles to it:
I am mainly concerned about it being non-intuitive. But I understand that having both suppressions feels unnecessarily verbose.
12 remaining items
abitrolly commentedon Apr 19, 2026
Push the exact mapping to the user side? I like that
tyerrors are more specific - likeinvalid-return-typeis less vague/more readable/specific thanreturn-value. The mapping would look like this:Using the mapping should still give user visible sign that the mapping is used. Maybe even error out in some strict mode.
alex commentedon Jul 4, 2026
FWIW, another reason this would be helpful is that if you run
mypywithwarn-unused-ignores, then# type: ignore[mypy-code, ty:ty-code]produces an unused ignore warning onmypy(for thety:ty-codeignore).I'm not sure if there's any effort to standardize the
tool:rulesyntax such thatmypyautomatically ignoresty:as "not for me"?AlexWaygood commentedon Jul 4, 2026
Mypy have said in the past that they consider that to be a mypy bug (or at least a missing feature); part of the reason why we switched our supported
type: ignorepatterns was that mypy said this was their preferred pattern for folks who run multiple type checkers on their codebasealex commentedon Jul 4, 2026
Their preferred pattern is a thing they don't support? I guess generously this is a "this is the right way to evolve the ecosystem" thing. I shall move my confusion to their issue tracker, thanks much.
AlexWaygood commentedon Jul 4, 2026
Correct. See e.g. https://discuss.python.org/t/allow-type-ignore-mypy-style-error-codes-in-conformance-tests/105433/11 and python/typing#2153 (comment) for mypy's position here
sobolevn commentedon Aug 14, 2026
This is a huge blocker for me in https://github.com/wemake-services/django-modern-rest/
We already support
mypy,pyright,pyreflyin strict modes with very small amount of custom per-typechecker ignores in the project. For example,pyreflyrequired just 18 custom ignores for the whole project.tynow has 251 errors. I can't realistically work through them. Most of them are just because of the error code mismatches.Examples:
and
The DX feels very complex.
MichaReiser commentedon Aug 14, 2026
I'd be supportive of this, depending on the complexity it introduces. There are a few cases worth calling out:
ty:ignorecomment should always take precedence over atype:ignorecomment (with or without code). This is unlike today, where ty always uses the closest suppression comment--add-ignoreshould not append to atype:ignorethat only uses mypy error codes (it's okay if it contains at least onetype:ignore[ty:code])type:ignore. E.g. so that it's explicit which type checker requires which suppressions?sobolevn commentedon Aug 14, 2026
Maybe introduce
--type-ignore-comment-style=ty|mypy|...? This way users can specify what exactly they want.MichaReiser commentedon Aug 14, 2026
That sounds nice, but is probably more complicated to implement because now you need to support that fully in
--add-ignore. We might also need to consider whether this replacesrespect-type-ignoresto avoid that the two options contradict each other.I think the conclusion internally is that this seems desirable but it needs design.