Narrow the remaining tuple type for sequence patterns with several refutable items - #21970
Open
ekanshul wants to merge 1 commit into
Open
Narrow the remaining tuple type for sequence patterns with several refutable items#21970ekanshul wants to merge 1 commit into
ekanshul wants to merge 1 commit into
Conversation
…futable items
For a fixed-length tuple subject, the remaining type after a sequence pattern
was only narrowed when at most one subpattern could fail to match. With two or
more refutable subpatterns (`case True, True:` on a `tuple[bool, bool]`) the
rest type stayed the whole tuple, so `match` statements over tuples of bools,
enums or literals were never exhaustive and `--enable-error-code
exhaustive-match` (and the missing-return check) complained.
The remaining values are the ones where some item fails to match while every
item before it matches, so build the rest type as the union of one tuple per
refutable position:
tuple[A, B] - tuple[a, b] == tuple[A - a, B] | tuple[a, B - b]
Positions whose subpattern always matches keep their type. The previous
single-position behaviour is the special case with one refutable item.
Fixes python#21968
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Fixes #21968
For a fixed-length tuple subject,
visit_sequence_patternonly narrowed the remaining type when at most one subpattern could fail to match (#20896). With two or more refutable subpatterns, e.g.case True, True:on atuple[bool, bool], the rest type stayed the whole tuple, somatchstatements over tuples of bools, enums or literals never became exhaustive:--enable-error-code exhaustive-matchreported an unhandled case for the fulltuple[bool, bool], and functions relying on the match to return got "Missing return statement".The values that don't match a sequence pattern are the ones where some item fails to match while every item before it matches, so the rest type is now the union of one tuple per refutable position:
Positions whose subpattern always matches (an empty rest) keep their type, and the previous single-position behaviour falls out as the special case with one refutable item. The union goes through
make_simplified_union, and a subject that already is a union of tuples is handled per item by the existing union branch, so the example from the issue now narrows totuple[Literal[False], Literal[False]]before its last case and toNeverafter it.One existing expectation changes: in
testMatchSequencePatternNegativeNarrowing, the rest ofcase (1, "a")ontuple[Literal[1, 2], Literal["a", "b"]]is nowtuple[Literal[2], Literal['a'] | Literal['b']] | tuple[Literal[1], Literal['b']]instead of the unnarrowed tuple.New tests cover the revealed rest types for two and three positions (including a wildcard in the middle and a subpattern that can't match at all), narrowing of the individual names for a
match a, b:subject, and exhaustiveness for tuples of bools and enums, including the more precise unhandled-case message.🤖 Generated with Claude Code