python / python/typing

Feature Request: Type hint for Fixed Length Homogeneous Sequences

Đang mở
#786 7 bình luận 47 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ả

Feature

A concise way to hint that a sequence of homogenous types is a set length. i.e The items in the sequence are all of the same type, the sequence is iterable, and cannot grow larger or smaller.

Pitch

Currently, the recommended way to add type hints to fixed-length sequences is to use Tuples 1. i.e

def foo(ten_floats: Tuple[float,float,float,float,float,float,float,float,float,float]):
    ...

For longer lists, this repetition is tedious and prone to error.
It would be nice to have a shorthand for defining homogeneous tuples. e.g

def foo(ten_floats: HomogeneousTuple[float, 10]):
    ...

Although the name Tuple does not signal intent very well. The real thing that we are trying to signal is that this is a sequence of floats of length 10. It does not necessarily have to be a Tuple, it just has to be iterable, and have 10 items, that cannot grow larger or smaller. If I wanted to pass the function a List, this should also pass static type checkers, as long as it has 10 floats.

Possible Implementation

I am not a core python developer, I have never even looked at the implementation of the typing module, and I'm not even that good of a python developer, so please forgive me if this is nonsense.

In Python 3.10 or newer [2] we will be able to use the unpack operator in subscript. So we might be able to do something like:

_t=[float]*10
def foo(ten_floats: Tuple[*_t]):
    ...

Using TypeVars, we could generalize this function to accept 10 of any type (as long as they are all the same type)

T=TypeVar('T')
TenTypeVars=[T]*10
def foo(ten_homogeneous_items: Tuple[*TenTypeVars]):
    ...

Following this logic, perhaps a generic type hint for Fixed Length Homogeneous Sequences could be implemented with

class FixedLengthHomogeneousSequence(Generic[T, N], Tuple[*[T]*N]):
    ...

FixedLengthHomogeneousSequence is a bit wordy. So perhaps FixedList would be a better name.

[2] (I think this is true). Pylance gave me an error saying this was the case. I think it's related to https://www.python.org/dev/peps/pep-0622/

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

Issue không xác định file triển khai, test hay entry point nào. Hãy bắt đầu bằng việc xem xét hành vi hiện có của module typing đối với Tuple, TypeVar và các generic sequence, sau đó so sánh cú pháp được đề xuất với PEP được tham chiếu; công việc được xem là hoàn tất khi đã thống nhất thiết kế type-checking, vị trí triển khai và các test bao phủ các sequence đồng nhất có độ dài cố định.

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
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
Cần làm rõ
Mức phù hợp với người mới
20/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.