fix support check more enum exhaustive #3294#3297
Open
asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
Open
fix support check more enum exhaustive #3294#3297asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
Conversation
This comment has been minimized.
This comment has been minimized.
|
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
This PR extends Pyrefly’s match-exhaustiveness checking so that certain non-enum builtin subjects (e.g., int, str, list[int]) can emit a “Match on T is not exhaustive” diagnostic, addressing issue #3294.
Changes:
- Added a regression test ensuring non-exhaustive matches on common builtins produce
NonExhaustiveMatcherrors. - Threaded a new
include_open_builtinsflag through binding → solver to enable exhaustiveness checking for selected builtin types. - Updated the exhaustiveness eligibility logic to optionally include several “open domain” builtins.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
pyrefly/lib/test/pattern_match.rs |
Adds regression coverage for non-exhaustive matches on builtins (int, str, list[int]). |
pyrefly/lib/binding/pattern.rs |
Computes and passes include_open_builtins into the match exhaustiveness expectation. |
pyrefly/lib/binding/binding.rs |
Extends BindingExpect::MatchExhaustiveness payload with include_open_builtins. |
pyrefly/lib/alt/solve.rs |
Propagates include_open_builtins to check_match_exhaustiveness. |
pyrefly/lib/alt/narrow.rs |
Expands should_check_exhaustiveness to optionally include selected builtin classes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+532
to
533
| let include_open_builtins = x.cases.len() > 1; | ||
| for case in x.cases { |
Comment on lines
1094
to
1105
| /// A match statement that may be non-exhaustive at runtime. | ||
| /// Due to gaps in our type algebra, we only check exhaustiveness for enums & unions | ||
| /// of enum literals. | ||
| /// Since this makes use of narrowing, not every match subject will be | ||
| /// checked for exhaustiveness, only variables and chained subscripts/attributes of variables | ||
| MatchExhaustiveness { | ||
| subject_idx: Idx<Key>, | ||
| narrowing_subject: NarrowingSubject, | ||
| narrow_ops_for_fall_through: (Box<NarrowOp>, TextRange), | ||
| subject_range: TextRange, | ||
| include_open_builtins: bool, | ||
| }, |
Comment on lines
1831
to
+1846
| Type::ClassType(cls) => { | ||
| // Final classes can't have subclasses, so they are exhaustible, with the exception | ||
| // of Flag enums, whose members can be combined into new members via bitwise ops | ||
| !self.is_flag_enum(cls) && self.is_final(cls.class_object()) | ||
| // bool is effectively Literal[True] | Literal[False] | ||
| || cls.is_builtin("bool") | ||
| || include_open_builtins | ||
| && (cls.is_builtin("bytearray") | ||
| || cls.is_builtin("bytes") | ||
| || cls.is_builtin("dict") | ||
| || cls.is_builtin("float") | ||
| || cls.is_builtin("frozenset") | ||
| || cls.is_builtin("int") | ||
| || cls.is_builtin("list") | ||
| || cls.is_builtin("set") | ||
| || cls.is_builtin("str") | ||
| || cls.is_builtin("tuple")) |
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 #3294
Test Plan