Skip to content

[match-case] fix matching against typing.Callable and Protocol types. #19471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

randolf-scholz
Copy link
Contributor

@randolf-scholz randolf-scholz commented Jul 17, 2025

Added extra logic in checker.py:conditional_types function to deal with structural types such as typing.Callable or protocols.

new tests

  • testMatchClassPatternCallable: tests case Callable() as fn usage
  • testMatchClassPatternProtocol: tests case Proto() usage, where Proto is a Protocol
  • testMatchClassPatternCallbackProtocol: tests case Proto() usage, where Proto is a Callback-Protocol
  • testGenericAliasIsinstanceUnreachable: derived from a mypy-primer failure in mesonbuild. Tests that isinstance(x, Proto) can produce unreachable error.
  • testGenericAliasRedundantExprCompoundIfExpr: derived from a CI failure of python runtest.py self of an earlier version of this PR.

modified tests

  • testOverloadOnProtocol added annotations to overload implementation, which wasn't getting checked. Added missing return. Fixed return type in second branch.

This comment has been minimized.

@randolf-scholz randolf-scholz marked this pull request as draft July 19, 2025 22:16

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

mitmproxy (https://github.com/mitmproxy/mitmproxy)
+ mitmproxy/http.py:607: error: Unused "type: ignore" comment  [unused-ignore]

@randolf-scholz randolf-scholz marked this pull request as ready for review July 20, 2025 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant