Skip to content

Consider best-effort supporting mypy error code names for some common cases #3127

Open

Description

@alex

Question

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

Activity

  1. added
    question
    on Mar 24, 2026
  2. carljm commented on Mar 24, 2026

    @carljm
    Contributor

    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.

  3. alex commented on Mar 24, 2026

    @alex
    Author

    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?

  4. carljm commented on Mar 25, 2026

    @carljm
    Contributor

    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.

  5. alex commented on Mar 25, 2026

    @alex
    Author

    FWIW, an example of a place where I wouldn't necessarily recommend trying to emulate is something like:

    def foo(x: list[int]): ...
    
    foo(["abc"])
    

    Right now mypy gives you a list-item error and ty gives you an arg-type :-)

  6. carljm commented on Mar 25, 2026

    @carljm
    Contributor

    Yeah, that's a pretty good example of why this code-matching would be best-effort at best.

  7. alex commented on Mar 25, 2026

    @alex
    Author

    Yeah. As a practical matter I think invalid-argument-type = arg-type captures like 90% of the value (for us it's 376/400 ty findings).

  8. 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
  9. added
    needs-decision
    and removed
    question
    on Mar 25, 2026
  10. added this to the Stable milestone on Mar 25, 2026
  11. carljm commented on Mar 25, 2026

    @carljm
    Contributor

    Another user reports that an equivalence between mypy's attr-defined and our unresolved-attribute would help them similarly.

  12. AlexWaygood commented on Mar 25, 2026

    @AlexWaygood
    Member

    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.

    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.)

  13. AlexWaygood commented on Mar 25, 2026

    @AlexWaygood
    Member

    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

  14. MichaReiser commented on Mar 25, 2026

    @MichaReiser
    Member

    I'm not opposed to it, but there are certainly some wrinkles to it:

    • It's less intuitive when a ty suppression is necessary in addition to mypy's because ty sometimes recognizes the ty code, but not always.
    • Seeing a mypy or ty suppression is a signal that this is a type checker limitation (or feature)
    • ty can't warn about unused ignore comments that use mypy's rule name and neither can mypy. Making stale suppression comments more likely.
    • I'm sure there are users who want to be explicit with their ignore codes, meaning we need an option to toggle this behavior.

    I am mainly concerned about it being non-intuitive. But I understand that having both suppressions feels unnecessarily verbose.

  15. 12 remaining items

  16. abitrolly commented on Apr 19, 2026

    @abitrolly

    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:

    [tool.ty.rule-mapping]
    return-value = "ty:invalid-return-type"

    Using the mapping should still give user visible sign that the mapping is used. Maybe even error out in some strict mode.

  17. modified the milestones: Stable, on May 22, 2026
  18. alex commented on Jul 4, 2026

    @alex
    Author

    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"?

  19. AlexWaygood commented on Jul 4, 2026

    @AlexWaygood
    Member

    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

  20. alex commented on Jul 4, 2026

    @alex
    Author

    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.

  21. AlexWaygood commented on Jul 4, 2026

    @AlexWaygood
    Member
  22. added
    diagnostics
    on Aug 4, 2026
  23. sobolevn commented on Aug 14, 2026

    @sobolevn

    This is a huge blocker for me in https://github.com/wemake-services/django-modern-rest/
    We already support mypy, pyright, pyrefly in strict modes with very small amount of custom per-typechecker ignores in the project. For example, pyrefly required just 18 custom ignores for the whole project.

    ty now has 251 errors. I can't realistically work through them. Most of them are just because of the error code mismatches.

    Examples:

    error[unresolved-attribute]: Unresolved attribute `__dmr_external_openapi__` on type `(...) -> Any`
      --> dmr/routing.py:61:9
       |
    61 |         self.url.callback.__dmr_external_openapi__ = self.openapi  # type: ignore[attr-defined]
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    and

    error[invalid-argument-type]: Argument to function `assert_async_throttling` is incorrect
      --> typesafety/assert_type/test_test/test_test_throttling.py:85:9
       |
    85 |         when='never',  # type: ignore[arg-type]
       |         ^^^^^^^^^^^^ Expected `Literal["any", "before_auth", "after_auth"]`, found `Literal["never"]`
    

    The DX feels very complex.

  24. MichaReiser commented on Aug 14, 2026

    @MichaReiser
    Member

    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?
  25. sobolevn commented on Aug 14, 2026

    @sobolevn

    Maybe introduce --type-ignore-comment-style=ty|mypy|...? This way users can specify what exactly they want.

  26. MichaReiser commented on Aug 14, 2026

    @MichaReiser
    Member

    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.

  27. added
    needs-design
    and removed
    needs-decision
    on Aug 14, 2026
  28. removed this from the milestone on Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsneeds-designsuppression

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions