python / python/typing_extensions

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

Đang mở
#103 2 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

pep-646
Ngôn ngữ chính
Python
Star
583
Fork
146
Merge trung bình
10 giờ 11 phút
Pull request đã merge (30 ngày)
5

Mô tả

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, ...]]].

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với ví dụ PEP 646 trong issue và so sánh hành vi của nó giữa các phiên bản Python được báo cáo khi dùng typing-extensions 4.4.0. Sau đó kiểm tra các trường hợp bị bỏ qua trong tests/test_pep_646.py quanh các dòng 392-408; hoàn thành khi các bí danh variadic lồng nhau tạo ra các đối số kiểu nhất quán trên các phiên bản cũ được hỗ trợ.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.