Add PartialApplication
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Bắt đầu với hành vi được đề xuất của typing.PartialApplication và so sánh nó với các khái niệm ParamSpec, Concatenate, get, functools.partial, typing và typing_extensions được nêu trong issue. Đọc cuộc thảo luận được liên kết và issue của MyPy để nắm các ràng buộc trước đây; công việc được xem là hoàn tất khi đạt được sự thống nhất về thiết kế và xác định được hành vi hỗ trợ cả các ví dụ partial lẫn jit.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Pitch
Add typing.PartialApplication to facilitate the implementation of:
__get__, andfunctools.partial,
both of which are practically impossible to natively (without plugins) annotate.
The __get__ method is currently handled internally by type checkers, and a MyPy plugin for partial has proved to be very difficult.
Proposal
I created a discussion, but I wanted to flesh this out as a new feature:
The idea is that PartialApplication takes three parameters:
- a
ParamSpecparameterP, - a tuple parameter
T, and - a dictionary parameter
D(defaulting to an empty dictionary).
It returns a new ParamSpec with all the arguments of P after removing
- the first
len(T)positional parameters, - the named keyword parameters from D.
It verifies that this removed parameters are all supertypes of the corresponding arguments, or else returns a type error.
Partial case study
An example with partial (might need some tweaks)
P = ParamSpec('P')
Q = ParamSpec('P')
R = TypeVar('R', covariant=True)
class partial(Generic[P, Q, R]):
S: TypeAlias = PartialApplication(P, Q.args, Q.kwargs)
def __init__(self, f: Callable[P, R], /, *args: Q.args, **kwargs: Q.kwargs): ...
def __call__(self, /, *args: S.args, **kwargs: S.kwargs) -> R: ...
Thus, calling partial(f, ...) would check the parameters, and produce a __call__ method with the right signature.
JIT example
Consider trying to create a decorator jit that works with both bare functions and methods. The problem is that in the method case, it has to respond to __get__ and strip off the first argument. It seems that we can only do this with Concatenate:
from typing import Callable, Generic, Protocol, TypeVar, overload, Any
from typing_extensions import ParamSpec, Self, Concatenate
V_co = TypeVar("V_co", covariant=True)
U = TypeVar("U", contravariant=True)
P = ParamSpec("P")
class Wrapped(Protocol, Generic[P, V_co]):
def __call__(self, /, *args: P.args, **kwargs: P.kwargs) -> V_co:
...
class WrappedMethod(Protocol, Generic[S, P, V_co]):
def __call__(self: S, *args: P.args, **kwargs: P.kwargs) -> V_co:
...
@overload
def __get__(self, instance: None, owner: Any = None) -> Self:
...
@overload
def __get__(self, instance: S, owner: Any = None) -> Wrapped[P, V_co]:
...
# this overload can only be hit if there is a positional parameter. It responds to `__get__` by
# throwing that parameter out.
@overload
def jit(f: Callable[Concatenate[U, P], V_co]) -> WrappedMethod[U, P, V_co]:
...
@overload
def jit(f: Callable[P, V_co]) -> Wrapped[P, V_co]:
...
def jit(f: Callable[..., Any]) -> Any:
...
class X:
@jit
def f(self, x: int) -> None:
pass
@jit
def g(x: int, y: float) -> None:
pass
x = X()
x.f(3)
x.f(x=3)
g(3, 4.2)
g(x=3, y=4.2) # Fails!
reveal_type(x.f)
reveal_type(g.__call__)
We can't seem to deal with the method case alongside the function case. Here's the proposed solution:
class Wrapped(Protocol, Generic[P, V_co]):
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> V_co:
...
def __get__(self, instance: U, owner: Any = None
) -> Callable[PartialApplication[P, tuple[U]], V_co]:
... # Much easier!
def jit(f: Callable[P, V_co]) -> Wrapped[P, V_co]:
pass # No overloads!
- Ngôn ngữ chính
- Python
- Star
- 1.8k
- Fork
- 302
- Merge trung bình
- 23 giờ
- Pull request đã merge (30 ngày)
- 8
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
- Đọ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.
Issue khác của python/typing
-
topic: typing spec
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
topic: typing spec
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
topic: documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
topic: documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
topic: conformance tests topic: typing spec
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
Tất cả issue của python/typing
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
OpenHands/extensions#626 · 1 bình luận ·
-
Change observation tooltip text Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
CSCfi/sd-search-api#39 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
please add to porn list Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
StevenBlack/hosts#3255 ·