fix Sequence pattern in match/case does not narrow tuple out of a union #3147#3165
Open
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
Open
fix Sequence pattern in match/case does not narrow tuple out of a union #3147#3165asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
Conversation
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
There was a problem hiding this comment.
Pull request overview
Fixes pyrefly’s match/case narrowing so that an irrefutable sequence pattern (e.g. (_, _)) properly participates in negative narrowing across subsequent case arms, allowing union exhaustiveness to be recognized (Issue #3147).
Changes:
- Add a regression test covering
float | tuple[float, float]exhaustiveness with a tuple-shaped sequence pattern plus afloat()class pattern. - In sequence-pattern binding, detect when all subpatterns are irrefutable/wildcards and strip
Placeholdernarrow ops so they don’t block negated narrowing across later cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
pyrefly/lib/test/pattern_match.rs |
Adds a regression test for match exhaustiveness/narrowing in the reported tuple-in-union scenario. |
pyrefly/lib/binding/pattern.rs |
Adjusts sequence-pattern narrowing to remove spurious placeholders when the sequence subpatterns are all irrefutable, improving cross-case narrowing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if all_subpatterns_irrefutable | ||
| && let Some(subject) = match_subject.as_single() | ||
| && let Some((op, _)) = narrow_ops.0.get_mut(subject.name()) | ||
| { |
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3147
Sequence patterns like (_, _) were picking up Placeholder narrows from wildcard and other irrefutable subpatterns, and those placeholders intentionally block negative narrowing.
In a later case, that prevented the fallback arm from collapsing to Never.
now strip those placeholders when every sequence subpattern is irrefutable, so the carried negation stays precise.
Test Plan
add test