-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Heterogeneous unpacking support for unions #20377
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cc7faa0
[ty] Heterogeneous unpacking support for unions
AlexWaygood 0ac4fd4
tests
AlexWaygood a932b3c
.
AlexWaygood c488648
more comments and tests
AlexWaygood c068c10
fix lint
AlexWaygood 8497f97
may as well?
AlexWaygood 14c7d82
remove untested code
AlexWaygood e2a21e2
improve
AlexWaygood 8dde577
address review
AlexWaygood 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
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
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 |
|---|---|---|
|
|
@@ -1106,10 +1106,14 @@ impl<'db> Bindings<'db> { | |
| // iterable (it could be a Liskov-uncompliant subtype of the `Iterable` class that sets | ||
| // `__iter__ = None`, for example). That would be badly written Python code, but we still | ||
| // need to be able to handle it without crashing. | ||
| overload.set_return_type(Type::tuple(TupleType::new( | ||
| db, | ||
| &argument.iterate(db), | ||
| ))); | ||
| let return_type = if let Type::Union(union) = argument { | ||
| union.map(db, |element| { | ||
| Type::tuple(TupleType::new(db, &element.iterate(db))) | ||
| }) | ||
| } else { | ||
| Type::tuple(TupleType::new(db, &argument.iterate(db))) | ||
| }; | ||
| overload.set_return_type(return_type); | ||
|
Comment on lines
+1109
to
+1116
Member
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. Not blocking for this PR, but this pattern seems to come up enough to deserve a helper method on |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -2309,6 +2313,12 @@ impl<'a, 'db> ArgumentMatcher<'a, 'db> { | |
| argument: Argument<'a>, | ||
| argument_type: Option<Type<'db>>, | ||
| ) -> Result<(), ()> { | ||
| // TODO: `Type::iterate` internally handles unions, but in a lossy way. | ||
| // It might be superior here to manually map over the union and call `try_iterate` | ||
| // on each element, similar to the way that `unpacker.rs` does in the `unpack_inner` method. | ||
| // It might be a bit of a refactor, though. | ||
| // See <https://github.com/astral-sh/ruff/pull/20377#issuecomment-3401380305> | ||
| // for more details. --Alex | ||
| let tuple = argument_type.map(|ty| ty.iterate(db)); | ||
| let (mut argument_types, length, variable_element) = match tuple.as_ref() { | ||
| Some(tuple) => ( | ||
|
|
||
Oops, something went wrong.
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.
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.
other than the
Type::Unionbranch, this is exactly the same code that used to exist in thematchstatement slightly lower down. It's just been extracted into a standalone function (so that it can be called recursively), and moved higher up to satisfy Clippy.