Skip to content
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

gh-118168: Fix Unpack interaction with builtin aliases #118169

Merged
merged 2 commits into from
Apr 23, 2024

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented 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")
Copy link
Member

@AlexWaygood AlexWaygood Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 type

But 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>
@JelleZijlstra
Copy link
Member Author

@AlexWaygood thanks for the review! Do you think this is safe to backport? (I'm leaning yes.)

@AlexWaygood
Copy link
Member

@AlexWaygood thanks for the review! Do you think this is safe to backport? (I'm leaning yes.)

yeah, I think should be fine

@AlexWaygood AlexWaygood added the needs backport to 3.12 only security fixes label Apr 23, 2024
@JelleZijlstra JelleZijlstra enabled auto-merge (squash) April 23, 2024 13:19
@JelleZijlstra JelleZijlstra merged commit d0b664e into python:main Apr 23, 2024
34 checks passed
@miss-islington-app
Copy link

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
…H-118169)

(cherry picked from commit d0b664e)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Apr 23, 2024

GH-118178 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Apr 23, 2024
AlexWaygood added a commit that referenced this pull request Apr 23, 2024
) (#118178)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@JelleZijlstra JelleZijlstra deleted the unpacktuple branch April 23, 2024 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants