python / python/mypy

How to inherit argument types from a specific wrapped function

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

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

feature
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

It would be great if there were a way to inherit the (argument) type signatures from one function to another. The use case here is something like:

def foo(
    something: str,
    *args: *requests.get.args,
    **kwargs: **requests.get.kwargs
):
    print(something)
    return requests.get(*args, **kwargs)

I've found https://github.com/python/mypy/issues/2003, though that was closed a while back. I'm not totally clear why that was closed (from the comments I can't work out what the solution was, nor if agreement was reached on an approach). Additionally a lot has changed since then with regards to argument typing -- notably ParamSpec, TypeVarTuple & Unpack now exist, though I'm not sure if any of them actually help here as the wrapped function is static rather than also being part of the call signature.

Ideally I'd like to be able to do this in cases where the original function is annotated, though without needing to change that function's annotations (e.g: because it's in a library), so using e.g: Unpack[TypedDict] would require me to repeat the definitions of the original function's signature, which is partly what I'm trying to avoid by using passthrough *args, **kwargs in the first place.

Aside: is "inherit" the right term for this? I wasn't sure what to search for.

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 bằng cách xem xét cú pháp được đề xuất trong issue này và cuộc thảo luận trước đó trong mypy issue #2003. So sánh xem ParamSpec, TypeVarTuple hoặc Unpack có giải quyết được việc kế thừa các annotation từ một hàm được bọc tĩnh như requests.get hay không; để hoàn tất sẽ cần một thiết kế được thống nhất và hành vi kiểm tra kiểu được ghi lại.

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