diff --git a/stdlib/functools.pyi b/stdlib/functools.pyi index 5db4c65bdb8d..7599f25f2d6b 100644 --- a/stdlib/functools.pyi +++ b/stdlib/functools.pyi @@ -3,7 +3,7 @@ import types 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, ParamSpec, Self, TypeAlias, final +from typing_extensions import Concatenate, Literal, ParamSpec, Self, TypeAlias, final if sys.version_info >= (3, 9): from types import GenericAlias @@ -30,6 +30,9 @@ if sys.version_info >= (3, 9): _T = TypeVar("_T") _S = TypeVar("_S") +_P = ParamSpec("_P") +_P1 = ParamSpec("_P1") +_R = TypeVar("_R") _PWrapped = ParamSpec("_PWrapped") _RWrapped = TypeVar("_RWrapped") _PWrapper = ParamSpec("_PWrapper") @@ -47,22 +50,36 @@ class _CacheInfo(NamedTuple): currsize: int @final -class _lru_cache_wrapper(Generic[_T]): - __wrapped__: Callable[..., _T] - def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ... +class _lru_cache_wrapper(Generic[_P, _R]): + __wrapped__: Callable[_P, _R] + def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: ... def cache_info(self) -> _CacheInfo: ... def cache_clear(self) -> None: ... - def __copy__(self) -> _lru_cache_wrapper[_T]: ... - def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[_T]: ... + def __copy__(self) -> _lru_cache_wrapper[_P, _R]: ... + def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[_P, _R]: ... + if sys.version_info >= (3, 10): + @overload + def __get__(self, instance: None, owner: type[Any] | None = ...) -> Self: ... + @overload + def __get__( + self: _lru_cache_wrapper[Concatenate[_S, _P1], _R], instance: _S, owner: type[Any] | None = ... + ) -> Callable[_P1, _R]: ... + else: + @overload + def __get__(self, instance: None, owner: type[Any] | None) -> Self: ... + @overload + def __get__( + self: _lru_cache_wrapper[Concatenate[_S, _P1], _R], instance: _S, owner: type[Any] | None + ) -> Callable[_P1, _R]: ... if sys.version_info >= (3, 8): @overload - def lru_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _T]], _lru_cache_wrapper[_T]]: ... + def lru_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[_P, _R]], _lru_cache_wrapper[_P, _R]]: ... @overload - def lru_cache(maxsize: Callable[..., _T], typed: bool = False) -> _lru_cache_wrapper[_T]: ... + def lru_cache(maxsize: Callable[_P, _R], typed: bool = False) -> _lru_cache_wrapper[_P, _R]: ... else: - def lru_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _T]], _lru_cache_wrapper[_T]]: ... + def lru_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[_P, _R]], _lru_cache_wrapper[_P, _R]]: ... WRAPPER_ASSIGNMENTS: tuple[ Literal["__module__"], Literal["__name__"], Literal["__qualname__"], Literal["__doc__"], Literal["__annotations__"] @@ -170,7 +187,7 @@ if sys.version_info >= (3, 8): def __class_getitem__(cls, item: Any) -> GenericAlias: ... if sys.version_info >= (3, 9): - def cache(__user_function: Callable[..., _T]) -> _lru_cache_wrapper[_T]: ... + def cache(__user_function: Callable[_P, _R]) -> _lru_cache_wrapper[_P, _R]: ... def _make_key( args: tuple[Hashable, ...],