gh-118168: Fix Unpack interaction with builtin aliases - #118169
Conversation
| if isinstance(arg, (_GenericAlias, types.GenericAlias)): | ||
| if arg.__origen__ is not tuple: | ||
| raise TypeError("Unpack[...] must be used with a tuple type") |
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>
|
@AlexWaygood thanks for the review! Do you think this is safe to backport? (I'm leaning yes.) |
yeah, I think should be fine |
|
Thanks @JelleZijlstra for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12. |
|
GH-118178 is a backport of this pull request to the 3.12 branch. |
Uh oh!
There was an error while loading. Please reload this page.