python / python/typing

Structural Pattern Matching for Types

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

I'm sure that there are other use cases for this, but the main one that comes to mind is inferring the type of function parameters. It is common that you want to override a superclass method, for example, which may accept many parameters that you don't need in the override:

class MyClass(BaseClass):
    def my_method(self, *args, **kwargs):
        ...
        return super().my_method(*args, **kwargs)

Unfortunately, most libraries, even if they provide type stubs, do not provide a TypedDict subclass that you can simply import in order to use Unpack[MyMethodKwargs], so the only way to not lose typing information is to redeclare all of the parameters.

If we could infer a ParamSpec type from the method type, and if we could infer the method type from the method object, then we could do something like:

class MyClass(BaseClass):
    def my_method(
        self,
        *args: Params[type[BaseClass.my_method]].args,
        **kwargs: Params[type[BaseClass.my_method]].kwargs,
    ):
        ...
        return super().my_method(*args, **kwargs)

I think that Python badly needs this, as **kwargs is so ubiquitous and one of the most notable places where type information is lost in my experience.

The way that TypeScript implements this is by leveraging pattern matching within conditional types:

type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;

This would be a very cool feature as it could be used for many other patterns, enabling things like Params[T], ReturnType[T], KeyOf[T], ValueOf[T], etc.

Unfortunately, since expressions were not implemented for structural pattern matching, we can't directly mirror the syntax, but it could maybe look something like:

type Params[C: Callable[..., Any]] = match C: P if case Callable[P, Any] else Never

or:

type Params[C: Callable[..., Any]] match C:
    case Callable[P, Any]: P
    case _: Never

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 hoặc bài kiểm thử nào trong repository được nêu tên. Hãy bắt đầu bằng việc xem xét các ví dụ ParamSpec và structural-pattern được đề xuất cùng với phương pháp TypeScript conditional-type được liên kết. Công việc được xem là hoàn tất khi có một đặc tả đã được thống nhất cho cú pháp, ngữ nghĩa và cách tương tác với các tính năng typing hiện có.

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, typescript
Lĩnh vực
developer-experience
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
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.