Skip to content

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

Merged
ilevkivskyi merged 23 commits into
python:masterfrom
randolf-scholz:fix_match_callable
Oct 16, 2025
Merged

ilevkivskyi merged 23 commits into
python:masterfrom
randolf-scholz:fix_match_callable

Conversation

@randolf-scholz

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

Copy link
Copy Markdown
Contributor

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.
@github-actions

This comment has been minimized.

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

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@randolf-scholz
randolf-scholz marked this pull request as ready for review July 20, 2025 10:49
Comment thread mypy/checker.py Resolved
Comment thread mypy/checker.py Resolved
Comment thread test-data/unit/check-python310.test Resolved
@sterliakov

Copy link
Copy Markdown
Collaborator

Also, does this fix #19470 completely or partially? Please update the description to either remove "partially" or remove "fixes" magic word, so that half-resolved issue does not get autoclosed

randolf-scholz and others added 4 commits July 27, 2025 18:46
Co-authored-by: Stanislav Terliakov <50529348+sterliakov@users.noreply.github.com>
Co-authored-by: Stanislav Terliakov <50529348+sterliakov@users.noreply.github.com>
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@sterliakov sterliakov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I like your docstring suggestion in the last comment, could you merge it?

@github-actions

This comment has been minimized.

@randolf-scholz

Copy link
Copy Markdown
Contributor Author

@sterliakov done.

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, looks like this is something worth trying

Comment thread mypy/checker.py Resolved
@github-actions

Copy link
Copy Markdown
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants