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

IntFloatTuple from PEP 646 doesn't work as expected on Python<3.11 #103

Open
Fatal1ty opened this issue Dec 19, 2022 · 2 comments
Open

IntFloatTuple from PEP 646 doesn't work as expected on Python<3.11 #103

Fatal1ty opened this issue Dec 19, 2022 · 2 comments
Labels

Comments

@Fatal1ty
Copy link

I was working on serialization / deserialization logic for the new PEP 646 in mashumaro, but unfortunately the following example from https://peps.python.org/pep-0646/#type-arguments-can-be-variadic doesn't work as expected on older python versions with typing-extensions 4.4.0:

from typing import Tuple, TypeVar
from typing_extensions import TypeVarTuple, Unpack, get_args


K = TypeVar("K")
T = TypeVar("T")
Ts = TypeVarTuple("Ts")

Ts1 = TypeVarTuple("Ts1")
Ts2 = TypeVarTuple("Ts2")
IntTuple = Tuple[int, Unpack[Ts1]]
IntFloatTuple = IntTuple[float, Unpack[Ts2]]

print(IntFloatTuple.__args__)
print(get_args(IntFloatTuple))

The PEP said:

Here, *Ts1 in the IntTuple alias is bound to Tuple[float, *Ts2], resulting in an alias IntFloatTuple equivalent to Tuple[int, float, *Ts2].

On python 3.11.0 it works seamlessly as expected:

print(IntFloatTuple.__args__)
# (<class 'int'>, <class 'float'>, *Ts2)

On python 3.10.9 it produces error on IntFloatTuple = IntTuple[float, Unpack[Ts2]]:

Traceback (most recent call last):
  File "/Users/alexander.tikhonov/projects/mashumaro/pep_646.py", line 12, in <module>
    IntFloatTuple = IntTuple[float, Unpack[Ts2]]
  File "/Users/alexander.tikhonov/.pyenv/versions/3.10.9/lib/python3.10/typing.py", line 309, in inner
    return cached(*args, **kwds)
  File "/Users/alexander.tikhonov/.pyenv/versions/3.10.9/lib/python3.10/typing.py", line 1070, in __getitem__
    arg = subst[arg]
KeyError: typing_extensions.Unpack[Ts1]

On python 3.9.16 it produces the same error on IntFloatTuple = IntTuple[float, Unpack[Ts2]]:

Traceback (most recent call last):
  File "/Users/alexander.tikhonov/projects/mashumaro/pep_646.py", line 12, in <module>
    IntFloatTuple = IntTuple[float, Unpack[Ts2]]
  File "/Users/alexander.tikhonov/.pyenv/versions/3.9.16/lib/python3.9/typing.py", line 274, in inner
    return cached(*args, **kwds)
  File "/Users/alexander.tikhonov/.pyenv/versions/3.9.16/lib/python3.9/typing.py", line 774, in __getitem__
    arg = subst[arg]
KeyError: typing_extensions.Unpack[Ts1]

On python 3.8.16 it produces wrong result:

print(IntFloatTuple.__args__)
(<class 'int'>, typing_extensions.Unpack[Ts1])
print(get_args(IntFloatTuple))
# (<class 'int'>, typing_extensions.Unpack[Ts1])

On python 3.7.16 it produces the same wrong result:

print(IntFloatTuple.__args__)
# (<class 'int'>, typing_extensions.Unpack[Ts1])
print(get_args(IntFloatTuple))
# (<class 'int'>, typing_extensions.Unpack[Ts1])

I couldn't figure out how to get type arguments the same on all python versions, and so it stops me from full support for variadic generics. At the moment I have to skip some tests like this one on python<3.11.

P.S. This problem also affects IntFloatsTuple = IntTuple[Unpack[Tuple[float, ...]]].

@JelleZijlstra
Copy link
Member

I added a reference to this limitation to the docs in #171. I'd still accept a PR fixing it.

@Daraan
Copy link
Contributor

Daraan commented Oct 22, 2024

As I've looked at this a bit I think that this can only be fixed via a monkey patch in typing._GenericAlias.__getitem__ to make this not raise an error in 3.9 & 3.10 and work like expected.
However I am not sure how much needs to be modified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants