-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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 12 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 | ||||
---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ import sys | |||||
import types | ||||||
from _typeshed import Self, SupportsAllComparisons, SupportsItems | ||||||
from typing import Any, Callable, Generic, Hashable, Iterable, NamedTuple, Sequence, Sized, TypeVar, overload | ||||||
from typing_extensions import Literal, final | ||||||
from typing_extensions import Literal, ParamSpec, final | ||||||
|
||||||
if sys.version_info >= (3, 9): | ||||||
from types import GenericAlias | ||||||
|
@@ -11,6 +11,10 @@ _AnyCallable = 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: ... | ||||||
|
@@ -44,8 +48,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] | ||||||
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: ... | ||||||
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)) # TypeError Same 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))) # TypeError So this should read:
Suggested change
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.
Show resolved
Hide resolved
|
||||||
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.