Union of ParamSpec?
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 20.6k
- Fork
- 3.3k
- Merge trung bình
- 1 ngày 18 giờ
- Pull request đã merge (30 ngày)
- 54
Mô tả
Feature
Currently it's possible to concatenate a type to a paramspec to get a new paramspec.
I think I may need the ability to concatenate two paramspecs, like:
P = ParamSpec('P')
Q = ParamSpec('Q')
R = TypeVar("R")
Callable[Concatenate[P, Q], R]
Pitch
I'm trying to describe partial-like functions, I spotted https://github.com/python/mypy/pull/16939 which solves if for functools.partial (which is good) but still not giving a way to describe other partial-like functions.
Currently I can successfully describe the "removal" of one or multiple parameters:
from collections.abc import Callable
from typing import Concatenate, Generic, ParamSpec, TypeVar
P = ParamSpec('P')
Q = ParamSpec('Q')
S = TypeVar('S')
T = TypeVar('T')
U = TypeVar('U')
def substract_one_parameter(given: T, func: Callable[Concatenate[T, P], U]) -> Callable[P, U]:
def inner(*args: P.args, **kwargs: P.kwargs) -> U:
return func(given, *args, **kwargs)
return inner
def substract_two_parameters(first: S, second: T, func: Callable[Concatenate[S, T, P], U]) -> Callable[P, U]:
def inner(*args: P.args, **kwargs: P.kwargs) -> U:
return func(first, second, *args, **kwargs)
return inner
def int_int_int(a: int, b: int) -> int:
return a + b
c = substract_one_parameter(1, int_int_int)
reveal_type(c) # def (b: builtins.int) -> builtins.int
print(c(2))
d = substract_two_parameters(1, 2, int_int_int)
reveal_type(d) # def () -> builtins.int
print(d())
But what I think I need is:
# P are the "removed" parameters
# Q are the "kept" parameters
def substract_n_parameters(func: Callable[Concatenate[P, Q], U], *outer_args: P.args, **outer_kwargs: P.kwargs) -> Callable[Q, U]:
def inner(*args: Q.args, **kwargs: Q.kwargs) -> U:
return func(*outer_args, *args, **outer_kwargs, **kwargs)
return inner
I feel that would cleanly express the "removal" of P from [P, Q], without the need to introduce a new function like Difference[P, Q].
I feel like this could also be used for the real functools.partial, maybe simplifying the current (working, yeah) implementation.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- 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 cách so sánh hành vi hiện có của ParamSpec và Concatenate với các ví dụ được cung cấp, sau đó xem xét triển khai functools.partial và pull request được tham chiếu. Công việc được xem là hoàn tất khi xác định được liệu có thể nối hai ParamSpec hay không và xác thực hành vi typing được đề xuất của substract_n_parameters.
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
- developer-experience, 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
- 35/100