Fix narrowing unions in tuple pattern matches #17109
Open
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.
Allows sequence pattern matching of tuple types with union members to use an unmatched pattern to narrow the possible type of union member to the type that was not matched in the sequence pattern.
For example given a type of
tuple[int, int | None]
a pattern match like:The case ..., None sequence pattern match can now narrow the type in further match statements to rule out the None side of the union.
This is implemented by moving the original implementation of tuple narrowing in sequence pattern matching since its functionality should be to the special case where a tuple only has length of one. This implementation does not hold for tuples of length greater than one since it does not account all combinations of alternative types.
This replaces that implementation with a new one that builds the rest type by iterating over the potential rest type members preserving narrowed types if they are available and replacing any uninhabited types with the original type of tuple member since these matches are only exhaustive if all members of the tuple are matched.
Fixes #14731