gh-118168: Fix Unpack interaction with builtin aliases#118169
Merged
JelleZijlstra merged 2 commits intopython:mainfrom Apr 23, 2024
Merged
gh-118168: Fix Unpack interaction with builtin aliases#118169JelleZijlstra merged 2 commits intopython:mainfrom
JelleZijlstra merged 2 commits intopython:mainfrom
Conversation
AlexWaygood
approved these changes
Apr 23, 2024
Comment on lines
+1789
to
+1791
| if isinstance(arg, (_GenericAlias, types.GenericAlias)): | ||
| if arg.__origin__ is not tuple: | ||
| raise TypeError("Unpack[...] must be used with a tuple type") |
Member
There was a problem hiding this comment.
It feels weirdly inconsistent that we allow nonsensical things with the new syntax, but not with the "backwards-compatible syntax":
>>> from typing import TypeVarTuple, Unpack
>>> class Foo[*Ts]: ...
...
>>> Ts = TypeVarTuple("Ts")
>>> Bar = Foo[int, *Ts]
>>> Bar[*list[str]]
__main__.Foo[int, *list[str]]
>>> Bar[Unpack[list[str]]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Bar[Unpack[list[str]]]
~~~^^^^^^^^^^^^^^^^^^^
File "/Users/alexw/dev/cpython/Lib/typing.py", line 394, in inner
return func(*args, **kwds)
~~~~^^^^^^^^^^^^^^^
File "/Users/alexw/dev/cpython/Lib/typing.py", line 1394, in __getitem__
args = _unpack_args(args)
~~~~~~~~~~~~^^^^^^
File "/Users/alexw/dev/cpython/Lib/typing.py", line 306, in _unpack_args
subargs = getattr(arg, '__typing_unpacked_tuple_args__', None)
~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/alexw/dev/cpython/Lib/typing.py", line 1791, in __typing_unpacked_tuple_args__
raise TypeError("Unpack[...] must be used with a tuple type")
TypeError: Unpack[...] must be used with a tuple typeBut I guess that's a preexisting problem, not new to this PR. We previously discussed the issue of "all generic aliases being unpackable" in #103450.
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Member
Author
|
@AlexWaygood thanks for the review! Do you think this is safe to backport? (I'm leaning yes.) |
Member
yeah, I think should be fine |
|
Thanks @JelleZijlstra for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12. |
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Apr 23, 2024
|
GH-118178 is a backport of this pull request to the 3.12 branch. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.