Skip to content

Commit

Permalink
Updated functools.wraps and functools.update_wrapper (#6670)
Browse files Browse the repository at this point in the history
Updated functools.wraps and functools.update_wrapper to use ParamSpec to preserve signatures of wrapped and wrapper functions.
  • Loading branch information
erictraut authored Feb 26, 2023
1 parent fee5f1b commit 9c4bfd5
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions stdlib/functools.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sys
import types
from _typeshed import IdentityFunction, SupportsAllComparisons, SupportsItems
from _typeshed import SupportsAllComparisons, SupportsItems
from collections.abc import Callable, Hashable, Iterable, Sequence, Sized
from typing import Any, Generic, NamedTuple, TypeVar, overload
from typing_extensions import Literal, Self, TypeAlias, final
from typing_extensions import Literal, ParamSpec, Self, TypeAlias, final

if sys.version_info >= (3, 9):
from types import GenericAlias
Expand All @@ -28,10 +28,12 @@ if sys.version_info >= (3, 8):
if sys.version_info >= (3, 9):
__all__ += ["cache"]

_AnyCallable: TypeAlias = Callable[..., object]

_T = TypeVar("_T")
_S = TypeVar("_S")
_PWrapped = ParamSpec("_PWrapped")
_RWrapped = TypeVar("_RWrapped")
_PWrapper = ParamSpec("_PWrapper")
_RWapper = TypeVar("_RWapper")

@overload
def reduce(function: Callable[[_T, _S], _T], sequence: Iterable[_S], initial: _T) -> _T: ...
Expand Down Expand Up @@ -67,8 +69,22 @@ 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] = ...) -> IdentityFunction: ...
class _Wrapped(Generic[_PWrapped, _RWrapped, _PWrapper, _RWapper]):
__wrapped__: Callable[_PWrapped, _RWrapped]
def __call__(self, *args: _PWrapper.args, **kwargs: _PWrapper.kwargs) -> _RWapper: ...

class _Wrapper(Generic[_PWrapped, _RWrapped]):
def __call__(self, f: Callable[_PWrapper, _RWapper]) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWapper]: ...

def update_wrapper(
wrapper: Callable[_PWrapper, _RWapper],
wrapped: Callable[_PWrapped, _RWrapped],
assigned: Sequence[str] = ...,
updated: Sequence[str] = ...,
) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWapper]: ...
def wraps(
wrapped: Callable[_PWrapped, _RWrapped], assigned: Sequence[str] = ..., updated: Sequence[str] = ...
) -> _Wrapper[_PWrapped, _RWrapped]: ...
def total_ordering(cls: type[_T]) -> type[_T]: ...
def cmp_to_key(mycmp: Callable[[_T, _T], int]) -> Callable[[_T], SupportsAllComparisons]: ...

Expand Down

0 comments on commit 9c4bfd5

Please sign in to comment.