python / python/typing

Specify methods that are allowed for inference of partially initialized generics

Đang mở
#1,989 1 bình luận 0 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ả

The inference of generic types for classes in most type-checkers assume that all the generic types can be inferred from the parameters to __init__. This is not always the case. For example, it is a common pattern to create an empty container class and add elements to it later, e.g.:

a = []
a.append("hello world")

Currently for builtin classes, such as list or dict, this idiom is understood by MyPy, but not by Pyright. Moreover, even MyPy is incapable of applying this type inference to custom types (see https://github.com/python/mypy/issues/13134). As mentioned in that issue, the problem is not only with empty containers, but also with some widely used APIs, such as the Estimator API from scikit-learn and related projects, in which the type of the data used is only known when the fit method is called.

My proposal would be to add functionality to the type system to define which methods can be used to infer generic types, apart from __init__. This could be made for example with a decorator (e.g. allow_generic_inference), so that one can define a method such as:

@allow_generic_inference
def append(value: T):
    ...

In this case the value of the class generic parameter T could be inferred from append if it was undefined before.

I think this proposal may also be similar to the previously suggested TypeAssert mentioned by @erictraut in https://github.com/python/typing/discussions/1013#discussioncomment-1966238. However I am not completely sure that TypeAssert could do what I explained here. I am also not sure that the proposed TypeAssert syntax is better than a decorator.

I hope you too consider this proposal useful, and I look forward to your feedback.

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 mã nguồn hoặc bài kiểm thử nào được nêu. Hãy bắt đầu bằng việc xem xét các ví dụ trong đề xuất về các container rỗng và các API Estimator của scikit-learn, sau đó đọc issue MyPy được liên kết và cuộc thảo luận về TypeAssert. Công việc được xem là hoàn tất khi đạt được thống nhất về việc liệu các phương thức ngoài init có thể suy luận các tham số generic hay không, đồng thời xác định cú pháp và hành vi được chấp nhận.

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
compilers
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
25/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.