-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Updated functools.wraps and functools.update_wrapper to use ParamSpec… #6670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
317fe5b
ea7f5cf
10900e2
97af48a
e0e7ea2
3901a15
c8374cf
27ddc44
dc71f76
2a20846
1d23078
2214a68
743f911
fad24de
aea60be
88d705f
1f7fcfc
823c32d
4d7dd16
2bab796
88f4904
fb51f0d
f2aa253
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,7 @@ import types | |||||
| from _typeshed import Self, SupportsAllComparisons, SupportsItems | ||||||
| from collections.abc import Callable, Hashable, Iterable, Sequence, Sized | ||||||
| from typing import Any, Generic, NamedTuple, TypeVar, overload | ||||||
| from typing_extensions import Literal, TypeAlias, final | ||||||
| from typing_extensions import Literal, ParamSpec, TypeAlias, final | ||||||
|
|
||||||
| if sys.version_info >= (3, 9): | ||||||
| from types import GenericAlias | ||||||
|
|
@@ -59,6 +59,10 @@ _AnyCallable: TypeAlias = Callable[..., Any] | |||||
|
|
||||||
| _T = TypeVar("_T") | ||||||
| _S = TypeVar("_S") | ||||||
| _P1 = ParamSpec("_P1") | ||||||
| _R1 = TypeVar("_R1") | ||||||
| _P2 = ParamSpec("_P2") | ||||||
| _R2 = TypeVar("_R2") | ||||||
|
|
||||||
| @overload | ||||||
| def reduce(function: Callable[[_T, _S], _T], sequence: Iterable[_S], initial: _T) -> _T: ... | ||||||
|
|
@@ -94,8 +98,17 @@ WRAPPER_ASSIGNMENTS: tuple[ | |||||
| ] | ||||||
| WRAPPER_UPDATES: tuple[Literal["__dict__"]] | ||||||
|
|
||||||
| def update_wrapper(wrapper: _T, wrapped: _AnyCallable, assigned: Sequence[str] = ..., updated: Sequence[str] = ...) -> _T: ... | ||||||
| def wraps(wrapped: _AnyCallable, assigned: Sequence[str] = ..., updated: Sequence[str] = ...) -> Callable[[_T], _T]: ... | ||||||
| class _Wrapped(Generic[_P1, _R1, _P2, _R2]): | ||||||
| __wrapped__: Callable[_P2, _R2] | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| def __call__(self, *args: _P1.args, **kwargs: _P1.kwargs) -> _R2: ... | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the other hand, from functools import update_wrapper
def wrapper(x: int) -> str:
return str(x)
def wrapped(y: str, z: bool) -> float:
return 42.0
wrapper2 = update_wrapper(wrapper, wrapped)
assert wrapper2 is wrapper
print(wrapper(13))
print(wrapper("", True)) # TypeErrorSame for from functools import wraps
def my_decorator(f):
@wraps(f)
def wrapper() -> int:
return 42
return wrapper
@my_decorator
def wrapped(x: int) -> bool:
return True
print(type(wrapped())) # -> class 'int'
print(type(wrapped(42))) # TypeErrorSo this should read:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Yes, after looking at your examples above, I realize that I had the ParamSpecs swapped in |
||||||
|
|
||||||
| class _Wrapper(Generic[_P1, _R1]): | ||||||
| def __call__(self, f: Callable[_P2, _R2]) -> _Wrapped[_P1, _R1, _P2, _R2]: ... | ||||||
|
|
||||||
| def update_wrapper( | ||||||
| wrapper: Callable[_P2, _R2], wrapped: Callable[_P1, _R1], assigned: Sequence[str] = ..., updated: Sequence[str] = ... | ||||||
| ) -> _Wrapped[_P1, _R1, _P2, _R2]: ... | ||||||
|
srittau marked this conversation as resolved.
Outdated
|
||||||
| def wraps(wrapped: Callable[_P1, _R1], assigned: Sequence[str] = ..., updated: Sequence[str] = ...) -> _Wrapper[_P1, _R1]: ... | ||||||
| def total_ordering(cls: type[_T]) -> type[_T]: ... | ||||||
| def cmp_to_key(mycmp: Callable[[_T, _T], int]) -> Callable[[_T], SupportsAllComparisons]: ... | ||||||
|
|
||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.