Repository navigation
Lenient handling of *tuple[Any, ...] (part 2) #22006
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
5 commits
Select commit
Hold shift + click to select a range
2ce9cb2
Lenient handling of *tuple[Any, ...] (part 2)
ilevkivskyi 33ab30e
Merge remote-tracking branch 'upstream/master' into fix-any-tvt-2
ilevkivskyi 6f2776a
A little refactoring
ilevkivskyi aaff0a4
Merge remote-tracking branch 'upstream/master' into fix-any-tvt-2
ilevkivskyi db20064
Dedicated error message; better test
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
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
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 |
|---|---|---|
|
|
@@ -1563,6 +1563,57 @@ x = c1 | |
| x = c2 | ||
| [builtins fixtures/tuple.pyi] | ||
|
|
||
| [case testAnyVariadicTupleLenientIndex] | ||
| from typing import Any, Unpack | ||
|
|
||
| t: tuple[int, Unpack[tuple[Any, ...]], str] | ||
| t1: tuple[int, Unpack[tuple[Any, ...]]] | ||
| t2: tuple[Unpack[tuple[Any, ...]], str] | ||
|
|
||
| reveal_type(t[42]) # N: Revealed type is "Any | builtins.str" | ||
| reveal_type(t[-42]) # N: Revealed type is "builtins.int | Any" | ||
|
|
||
| reveal_type(t1[42]) # N: Revealed type is "Any" | ||
| reveal_type(t1[-42]) # N: Revealed type is "builtins.int | Any" | ||
|
|
||
| reveal_type(t2[42]) # N: Revealed type is "Any | builtins.str" | ||
| reveal_type(t2[-42]) # N: Revealed type is "Any" | ||
|
|
||
| # Slicing support for variadic tuples is currently best effort, the key idea is to not give an error. | ||
| reveal_type(t[1:3:2]) # N: Revealed type is "builtins.tuple[builtins.int | Any | builtins.str, ...]" | ||
| reveal_type(t1[1:3:2]) # N: Revealed type is "builtins.tuple[builtins.int | Any, ...]" | ||
| reveal_type(t2[1:3:2]) # N: Revealed type is "builtins.tuple[Any | builtins.str, ...]" | ||
| bt = t[::0] # E: Slice step cannot be zero | ||
| reveal_type(bt) # N: Revealed type is "builtins.tuple[Any, ...]" | ||
| [builtins fixtures/tuple.pyi] | ||
|
|
||
| [case testAnyVariadicTupleLenientUnpacking] | ||
| from typing import Any, Unpack | ||
|
|
||
| t: tuple[int, Unpack[tuple[Any, ...]], str] | ||
|
|
||
| x0, y0 = t | ||
| reveal_type(x0) # N: Revealed type is "builtins.int" | ||
| reveal_type(y0) # N: Revealed type is "builtins.str" | ||
|
|
||
| x, y, z = t | ||
| reveal_type(x) # N: Revealed type is "builtins.int" | ||
| reveal_type(y) # N: Revealed type is "Any" | ||
| reveal_type(z) # N: Revealed type is "builtins.str" | ||
|
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. Test also the case with two lvalues (tuple expands to empty). |
||
|
|
||
| x1, y1, *zz, x2, y2 = t | ||
| reveal_type(x1) # N: Revealed type is "builtins.int" | ||
| reveal_type(y1) # N: Revealed type is "Any" | ||
| reveal_type(zz) # N: Revealed type is "builtins.list[Any]" | ||
| reveal_type(x2) # N: Revealed type is "Any" | ||
| reveal_type(y2) # N: Revealed type is "builtins.str" | ||
|
|
||
| t2: tuple[int, int, Unpack[tuple[Any, ...]], str, str] | ||
|
|
||
| # Not the best error, but this definitely invalid, even with Any. | ||
| x3, y3, z3 = t2 # E: Variadic tuple unpacking requires a star target | ||
| [builtins fixtures/tuple.pyi] | ||
|
|
||
| [case testUnpackingVariadicTuplesTypeVar] | ||
| from typing import Tuple | ||
| from typing_extensions import TypeVarTuple, Unpack | ||
|
|
||
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.
Should
t1[::0]generate an error, since it doesn't work at runtime (and doesn't make sense)?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.
Yes, we do give an error for
0stride (but the error message is bad). I will probably change the logic slightly to give a better error message.