Skip to content

Commit a00fcba

Browse files
authored
Revert "Revert use of ParamSpec for functools.wraps" (#16942)
ParamSpec support has improved so it doesn't seem necessary to revert the changes any more.
1 parent ea49e1f commit a00fcba

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

misc/generate_changelog.py

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ def filter_omitted_commits(commits: list[CommitInfo]) -> list[CommitInfo]:
7979
"Revert sum literal integer change",
8080
"Remove use of LiteralString in builtins",
8181
"Revert typeshed ctypes change",
82-
"Revert use of `ParamSpec` for `functools.wraps`",
8382
)
8483
):
8584
# These are generated by a typeshed sync.

misc/sync-typeshed.py

-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ def main() -> None:
182182
"d25e4a9eb", # LiteralString reverts
183183
"d132999ba", # sum reverts
184184
"dd12a2d81", # ctypes reverts
185-
"0dd4b6f75", # ParamSpec for functools.wraps
186185
]
187186
for commit in commits_to_cherry_pick:
188187
try:

mypy/typeshed/stdlib/functools.pyi

+26-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import sys
22
import types
3-
from _typeshed import IdentityFunction, SupportsAllComparisons, SupportsItems
3+
from _typeshed import SupportsAllComparisons, SupportsItems
44
from collections.abc import Callable, Hashable, Iterable, Sequence, Sized
55
from typing import Any, Generic, Literal, NamedTuple, TypedDict, TypeVar, final, overload
6-
from typing_extensions import Self, TypeAlias
6+
from typing_extensions import ParamSpec, Self, TypeAlias
77

88
if sys.version_info >= (3, 9):
99
from types import GenericAlias
@@ -27,11 +27,13 @@ __all__ = [
2727
if sys.version_info >= (3, 9):
2828
__all__ += ["cache"]
2929

30-
_AnyCallable: TypeAlias = Callable[..., object]
31-
3230
_T = TypeVar("_T")
3331
_T_co = TypeVar("_T_co", covariant=True)
3432
_S = TypeVar("_S")
33+
_PWrapped = ParamSpec("_PWrapped")
34+
_RWrapped = TypeVar("_RWrapped")
35+
_PWrapper = ParamSpec("_PWrapper")
36+
_RWrapper = TypeVar("_RWrapper")
3537

3638
@overload
3739
def reduce(__function: Callable[[_T, _S], _T], __sequence: Iterable[_S], __initial: _T) -> _T: ...
@@ -81,31 +83,41 @@ else:
8183
]
8284
WRAPPER_UPDATES: tuple[Literal["__dict__"]]
8385

86+
class _Wrapped(Generic[_PWrapped, _RWrapped, _PWrapper, _RWrapper]):
87+
__wrapped__: Callable[_PWrapped, _RWrapped]
88+
def __call__(self, *args: _PWrapper.args, **kwargs: _PWrapper.kwargs) -> _RWrapper: ...
89+
# as with ``Callable``, we'll assume that these attributes exist
90+
__name__: str
91+
__qualname__: str
92+
93+
class _Wrapper(Generic[_PWrapped, _RWrapped]):
94+
def __call__(self, f: Callable[_PWrapper, _RWrapper]) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWrapper]: ...
95+
8496
if sys.version_info >= (3, 12):
8597
def update_wrapper(
86-
wrapper: _T,
87-
wrapped: _AnyCallable,
98+
wrapper: Callable[_PWrapper, _RWrapper],
99+
wrapped: Callable[_PWrapped, _RWrapped],
88100
assigned: Sequence[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__", "__type_params__"),
89101
updated: Sequence[str] = ("__dict__",),
90-
) -> _T: ...
102+
) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWrapper]: ...
91103
def wraps(
92-
wrapped: _AnyCallable,
104+
wrapped: Callable[_PWrapped, _RWrapped],
93105
assigned: Sequence[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__", "__type_params__"),
94106
updated: Sequence[str] = ("__dict__",),
95-
) -> IdentityFunction: ...
107+
) -> _Wrapper[_PWrapped, _RWrapped]: ...
96108

97109
else:
98110
def update_wrapper(
99-
wrapper: _T,
100-
wrapped: _AnyCallable,
111+
wrapper: Callable[_PWrapper, _RWrapper],
112+
wrapped: Callable[_PWrapped, _RWrapped],
101113
assigned: Sequence[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__"),
102114
updated: Sequence[str] = ("__dict__",),
103-
) -> _T: ...
115+
) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWrapper]: ...
104116
def wraps(
105-
wrapped: _AnyCallable,
117+
wrapped: Callable[_PWrapped, _RWrapped],
106118
assigned: Sequence[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__"),
107119
updated: Sequence[str] = ("__dict__",),
108-
) -> IdentityFunction: ...
120+
) -> _Wrapper[_PWrapped, _RWrapped]: ...
109121

110122
def total_ordering(cls: type[_T]) -> type[_T]: ...
111123
def cmp_to_key(mycmp: Callable[[_T, _T], int]) -> Callable[[_T], SupportsAllComparisons]: ...

0 commit comments

Comments
 (0)