FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix matching against union of tuples by saulshanabrook · Pull Request #19600 · python/mypy · GitHub

Repository navigation

Fix matching against union of tuples - #19600

Merged
hauntsaninja merged 15 commits into
python:masterfrom
saulshanabrook:issue-19599
Feb 5, 2026
Merged

hauntsaninja merged 15 commits into
python:masterfrom
saulshanabrook:issue-19599

Conversation

saulshanabrook commented Aug 6, 2025 •
edited by hauntsaninja
Loading

Copy link
Copy Markdown
Contributor

This pull request fixes handling of union types containing tuples in match statements. Previously, when a tuple was part of a union, all its items would be unioned together and treated as a homogeneous tuple of that union type, which was incorrect.

It still fallbacks on this behavior if we there are multiple tuples in the union with Unpack in them, but otherwise now it should be handled correctly.

I attempted to keep as much of the existing semantics the same besides for this change. I also tried to keep the performance roughly similar, not unioning types more than needed.

Fixes #19599
Fixes #19082

saulshanabrook marked this pull request as draft August 6, 2025 20:22

This comment has been minimized.

saulshanabrook marked this pull request as ready for review August 6, 2025 20:40

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Copy Markdown
Contributor Author

This is now ready for review.

Comment thread mypy/checkpattern.py Outdated
elif not tuple_types and not sequence_types and not unknown_type:
return self.early_non_match()
elif tuple_types:
inner_types = [make_simplified_union([*sequence_types, *x]) for x in zip(*tuple_types)]

hauntsaninja Feb 5, 2026 •
edited
Loading

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think this zip isn't quite right, I will push the fix + test

Copilot AI review requested due to automatic review settings February 5, 2026 06:29

This comment was marked as outdated.

github-actions Bot commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

hauntsaninja merged commit 7fdbed0 into python:master Feb 5, 2026
23 checks passed

Copy link
Copy Markdown
Collaborator

Thank you!

hauntsaninja added a commit to hauntsaninja/mypy that referenced this pull request Feb 5, 2026
I realised there is a simpler and more powerful approach than the one
taken in python#19600. This basically adds the new "Step 2" to the original
code.
hauntsaninja added a commit to hauntsaninja/mypy that referenced this pull request Feb 5, 2026
I realised there is a simpler and more powerful approach than the one
taken in python#19600. This basically adds the new "Step 2" to the original
code.

Copy link
Copy Markdown
Contributor Author

Thank you for fixing this up! Excited to have this working now :)

hauntsaninja added a commit that referenced this pull request Feb 11, 2026
I realised there is a simpler and more complete approach than the one
taken in #19600.
This adds the new "Step 2" to the original code. Best way to review the
diff is probably to check it out and review it squashed with #19600 ,
but really the net logic change is just "Step 2"

Fixes #15190
Fixes #17549
Fixes #17600
Mostly fixes #18039
Helps with things in #19081
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Matching tuple against union merges field types Type narrowing for a tuple is worse with match than with the equivalent isinstance check

3 participants


Back | FazBrowse Home | New Git URL