-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Subtyping and inference of user defined variadic types #16076
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 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a1a0167
Handle various edge cases for homogeneous tuples
ilevkivskyi 37a28df
Start working on instances
ilevkivskyi d8ce5c5
Handle meets and joins
ilevkivskyi 31e82f8
Update constraint inference
ilevkivskyi 75a9024
Add some comments
ilevkivskyi 38cd66c
Update comment
ilevkivskyi 60df996
Some more fixes
ilevkivskyi 4991a9a
Update solver; update/add a TODO
8000
ilevkivskyi 7fc37da
Fix overload check; update upper bound in couple more places
ilevkivskyi f408038
Fix incremental mode (also don't visit void)
ilevkivskyi 11f1c76
Fix couple more crashes
ilevkivskyi ab3c2cc
Add a test
ilevkivskyi 6946a49
Add (and clean-up) tests
ilevkivskyi 7917bea
Meets/joins tests; fix silly bug; handle some more edge cases
ilevkivskyi 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
Add some comments
- Loading branch information
commit 75a9024de83ab5ef05c86162058a9ef0553b04ec
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
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
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 |
---|---|---|
|
@@ -724,6 +724,9 @@ def visit_instance(self, t: Instance) -> ProperType: | |
# N.B: We use zip instead of indexing because the lengths might have | ||
# mismatches during daemon reprocessing. | ||
if t.type.has_type_var_tuple_type: | ||
# We handle meet of variadic instances by simply creating correct mapping | ||
# for type arguments and compute the individual meets same as for regular | ||
# instances. All the heavy lifting is done in the meet of tuple types. | ||
s = self.s | ||
assert s.type.type_var_tuple_prefix is not None | ||
assert s.type.type_var_tuple_suffix is not None | ||
|
@@ -746,6 +749,8 @@ def visit_instance(self, t: Instance) -> ProperType: | |
for ta, sa, tv in zip(t_args, s_args, t.type.defn.type_vars): | ||
meet = self.meet(ta, sa) | ||
if isinstance(tv, TypeVarTupleType): | ||
# Correctly unpack possible outcomes of meets of tuples: it can be | ||
# either another tuple type or Never (normalized as *tuple[Never, ...]) | ||
if isinstance(meet, TupleType): | ||
args.extend(meet.items) | ||
continue | ||
|
@@ -842,6 +847,14 @@ def visit_overloaded(self, t: Overloaded) -> ProperType: | |
return meet_types(t.fallback, s) | ||
|
||
def meet_tuples(self, s: TupleType, t: TupleType) -> list[Type] | None: | ||
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. What about adding more unit tests for |
||
"""Meet two tuple types while handling variadic entries. | ||
|
||
This is surprisingly tricky, and we don't handle some tricky corner cases. | ||
Most of the trickiness comes from the variadic tuple items like *tuple[X, ...] | ||
since they can have arbitrary partial overlaps (while *Ts can't be split). This | ||
function is roughly a mirror of join_tuples() w.r.t. to the fact that fixed | ||
tuples are subtypes of variadic ones but not vice versa. | ||
""" | ||
s_unpack_index = find_unpack_in_list(s.items) | ||
t_unpack_index = find_unpack_in_list(t.items) | ||
if s_unpack_index is None and t_unpack_index is None: | ||
|
@@ -852,7 +865,12 @@ def meet_tuples(self, s: TupleType, t: TupleType) -> list[Type] | None: | |
return items | ||
return None | ||
if s_unpack_index is not None and t_unpack_index is not None: | ||
# TODO: handle more cases (like strictly shorter prefix/suffix). | ||
# The only simple case we can handle if both tuples are variadic | ||
# is when they are purely variadic. Other cases are tricky because | ||
# a variadic item is effectively a union of tuples of all length, thus | ||
# potentially causing overlap between a suffix in `s` and a prefix | ||
# in `t` (see how this is handled in is_subtype() for details). | ||
# TODO: handle more cases (like when both prefix/suffix are strictly shorter). | ||
if s.length() == 1 and t.length() == 1: | ||
s_unpack = s.items[0] | ||
assert isinstance(s_unpack, UnpackType) | ||
|
@@ -876,6 +894,7 @@ def meet_tuples(self, s: TupleType, t: TupleType) -> list[Type] | None: | |
variadic = t | ||
unpack_index = t_unpack_index | ||
fixed = s | ||
# If one tuple is variadic one, and the other one is fixed, the meet will be fixed. | ||
unpack = variadic.items[unpack_index] | ||
assert isinstance(unpack, UnpackType) | ||
unpacked = get_proper_type(unpack.type) | ||
|
@@ -913,6 +932,8 @@ def visit_tuple_type(self, t: TupleType) -> ProperType: | |
# A named tuple that inherits from a normal class | ||
return t | ||
elif self.s.type.has_type_var_tuple_type and is_subtype(t, self.s): | ||
# This is a bit ad-hoc but more principled handling is tricky, and this | ||
# special case is important for type narrowing in binder to work. | ||
return t | ||
return self.default(self.s) | ||
|
||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about adding more unit tests for join_tuples? This is pretty tricky but should be easy to unit test. This can happen in a follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a good idea after all. Tests caught a silly bug (missing early return in one place), and also gave me an idea of how to handle some more edge cases essentially for free.