python / python/typing

TypeVarTuple Transformations Before Unpack

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

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

topic: feature
Ngôn ngữ chính
Python
Star
1.8k
Fork
302
Merge trung bình
23 giờ
Pull request đã merge (30 ngày)
8

Mô tả

It would be useful to be able to apply type transformations to TypeVarTuple's types. Some motivating examples:

 Ts = TypeVarTuple('Ts')

 class Example(Generic[*Ts]):
     def __init__(self, *types: *type[Ts]) -> None:
         self.types = types

     def create_defaults(self) -> tuple[*Ts]:
         return tuple(t() for t in self.types)

     def create_lists(self) -> tuple[*list[Ts]]:
         return tuple([t()] for t in self.types)

     def create_callables(self) -> tuple[*Callable[[], Ts]]:
         return tuple(lambda: t() for t in self.types)

 e = Example(int, str)

 assert_type(e.create_defaults(), tuple[int, str])
 assert_type(e.create_lists(), tuple[list[int], list[str]])
 assert_type(e.create_callables(), tuple[Callable[[], int], Callable[[], str]])

At a high level, everything between the * and the TypeVarTuple it expands would be applied per type in the tuple before it's unpacked.

There are some decisions to be made about how the expansion is done if there are multiple TypeVarTuples in the expand expression, but I think it can be supported and I think the intuitive thing would be to zip up all the TypeVarTuples. For example,
tuple[*tuple[Ts, Us]] with Ts = (int, str) and Us = (float, bytes) would be tuple[tuple[int, float], tuple[str, bytes]], with the implementation enforcing equal-length TypeVarTuples.

If there's a workaround for this using the existing logic, let me know. My current use case involves keeping a reference to the runtime types, so *type[Ts] is what I'm looking for at the moment.

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

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

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

Không có tệp, kiểm thử hoặc điểm vào nào được nêu tên. Hãy bắt đầu bằng việc xem xét hành vi unpacking TypeVarTuple hiện có và các ví dụ làm động lực, sau đó xác định cách áp dụng các phép biến đổi trước khi unpacking, bao gồm ngữ nghĩa zip có cùng độ dài được đề xuất cho nhiều TypeVarTuples. Được xem là hoàn tất khi hành vi của tính năng và các quyết định mở về việc mở rộng được đặc tả đủ rõ ràng để có thể triển khai.

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
devtools
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
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
30/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.