-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Fix matching against union of tuples #19600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
003686d
Add failing test for #19599
saulshanabrook 6a179f7
Add more comphrenensive tests
saulshanabrook 853b762
almost working
saulshanabrook 2e0ce80
Fi test
saulshanabrook 13fc8a6
Format
saulshanabrook 543d224
Fix tests by handling object properly
saulshanabrook c9d1233
Simplify logic
saulshanabrook 9ca630c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ebc59ef
fix mypy error from overlapping variable
saulshanabrook 64a6f03
black
saulshanabrook 5a6464a
fix spelling error
saulshanabrook 89ffdb3
fix zip
hauntsaninja f1a9524
Merge remote-tracking branch 'origin/master' into issue-19599
hauntsaninja 53b58ac
update tests
hauntsaninja 6fe89be
nit
hauntsaninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,35 +244,77 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType: | |
|
|
||
| # | ||
| # get inner types of original type | ||
| # | ||
| # 1. Go through all possible types and filter to only those which are sequences that could match that number of items | ||
| # 2. If there is exactly one tuple left with an unpack, then use that type and the unpack index | ||
| # 3. Otherwise, take the product of the item types so that each index can have a unique type. For tuples with unpack | ||
| # fallback to merging all of their types for each index since we can't handle multiple unpacked items at once yet. | ||
|
|
||
| # Whether we have encountered a type that we don't know how to handle in the union | ||
| unknown_type = False | ||
| # A list of types that could match any of the items in the sequence. | ||
| sequence_types: list[Type] = [] | ||
| # A list of tuple types that could match the sequence, per index | ||
| tuple_types: list[list[Type]] = [] | ||
| # A list of all the unpack tuple types that we encountered, each containing the tuple type, unpack index, and union index | ||
| unpack_tuple_types: list[tuple[TupleType, int, int]] = [] | ||
| for i, t in enumerate( | ||
| current_type.items if isinstance(current_type, UnionType) else [current_type] | ||
| ): | ||
| t = get_proper_type(t) | ||
| if isinstance(t, TupleType): | ||
| t_items = list(t.items) | ||
| unpack_index = find_unpack_in_list(t_items) | ||
| if unpack_index is None: | ||
| size_diff = len(t_items) - required_patterns | ||
| if size_diff < 0: | ||
| continue | ||
| elif size_diff > 0 and star_position is None: | ||
| continue | ||
| elif not size_diff and star_position is not None: | ||
| t_items.append(UninhabitedType()) | ||
| tuple_types.append(t_items) | ||
| else: | ||
| normalized_inner_types = [] | ||
| for it in t_items: | ||
| # Unfortunately, it is not possible to "split" the TypeVarTuple | ||
| # into individual items, so we just use its upper bound for the whole | ||
| # analysis instead. | ||
| if isinstance(it, UnpackType) and isinstance(it.type, TypeVarTupleType): | ||
| it = UnpackType(it.type.upper_bound) | ||
| normalized_inner_types.append(it) | ||
| t_items = normalized_inner_types | ||
| t = t.copy_modified(items=normalized_inner_types) | ||
| if len(t_items) - 1 > required_patterns and star_position is None: | ||
| continue | ||
| unpack_tuple_types.append((t, unpack_index, i)) | ||
| # add the combined tuple type to the sequence types in case we have multiple unpacks we want to combine them all | ||
| sequence_types.append(self.chk.iterable_item_type(tuple_fallback(t), o)) | ||
| elif isinstance(t, AnyType): | ||
| sequence_types.append(AnyType(TypeOfAny.from_another_any, t)) | ||
| elif self.chk.type_is_iterable(t) and isinstance(t, Instance): | ||
| sequence_types.append(self.chk.iterable_item_type(t, o)) | ||
| else: | ||
| unknown_type = True | ||
| # if we only got one unpack tuple type, we can use that | ||
| unpack_index = None | ||
| if isinstance(current_type, TupleType): | ||
| inner_types = current_type.items | ||
| unpack_index = find_unpack_in_list(inner_types) | ||
| if unpack_index is None: | ||
| size_diff = len(inner_types) - required_patterns | ||
| if size_diff < 0: | ||
| return self.early_non_match() | ||
| elif size_diff > 0 and star_position is None: | ||
| return self.early_non_match() | ||
| if len(unpack_tuple_types) == 1 and len(sequence_types) == 1 and not tuple_types: | ||
| update_tuple_type, unpack_index, union_index = unpack_tuple_types[0] | ||
| inner_types: list[Type] = update_tuple_type.items | ||
| if isinstance(current_type, UnionType): | ||
| union_items = list(current_type.items) | ||
| union_items[union_index] = update_tuple_type | ||
| current_type = current_type.copy_modified(items=union_items) | ||
| else: | ||
| normalized_inner_types = [] | ||
| for it in inner_types: | ||
| # Unfortunately, it is not possible to "split" the TypeVarTuple | ||
| # into individual items, so we just use its upper bound for the whole | ||
| # analysis instead. | ||
| if isinstance(it, UnpackType) and isinstance(it.type, TypeVarTupleType): | ||
| it = UnpackType(it.type.upper_bound) | ||
| normalized_inner_types.append(it) | ||
| inner_types = normalized_inner_types | ||
| current_type = current_type.copy_modified(items=normalized_inner_types) | ||
| if len(inner_types) - 1 > required_patterns and star_position is None: | ||
| return self.early_non_match() | ||
| current_type = update_tuple_type | ||
| # if we only got tuples we can't match, then exit early | ||
| 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)] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this zip isn't quite right, I will push the fix + test |
||
| else: | ||
| inner_type = self.get_sequence_type(current_type, o) | ||
| if inner_type is None: | ||
| inner_type = self.chk.named_type("builtins.object") | ||
| inner_types = [inner_type] * len(o.patterns) | ||
| object_type = self.chk.named_type("builtins.object") | ||
| unioned = make_simplified_union(sequence_types) if sequence_types else object_type | ||
| inner_types = [unioned] * len(o.patterns) | ||
|
|
||
| # | ||
| # match inner patterns | ||
|
|
@@ -351,25 +393,6 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType: | |
| new_type = self.narrow_sequence_child(current_type, new_inner_type, o) | ||
| return PatternType(new_type, rest_type, captures) | ||
|
|
||
| def get_sequence_type(self, t: Type, context: Context) -> Type | None: | ||
| t = get_proper_type(t) | ||
| if isinstance(t, AnyType): | ||
| return AnyType(TypeOfAny.from_another_any, t) | ||
| if isinstance(t, UnionType): | ||
| items = [self.get_sequence_type(item, context) for item in t.items] | ||
| not_none_items = [item for item in items if item is not None] | ||
| if not_none_items: | ||
| return make_simplified_union(not_none_items) | ||
| else: | ||
| return None | ||
|
|
||
| if self.chk.type_is_iterable(t) and isinstance(t, (Instance, TupleType)): | ||
| if isinstance(t, TupleType): | ||
| t = tuple_fallback(t) | ||
| return self.chk.iterable_item_type(t, context) | ||
| else: | ||
| return None | ||
|
|
||
| def contract_starred_pattern_types( | ||
| self, types: list[Type], star_pos: int | None, num_patterns: int | ||
| ) -> list[Type]: | ||
|
|
||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.