From ef5b9aec1f20425b79e3348fc66c6884a888aaac Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Mon, 20 Feb 2023 14:31:45 +0100 Subject: [PATCH 1/6] Add typing for decorated function arguments in functools.cache() any functools.lru_cache() --- stdlib/functools.pyi | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/stdlib/functools.pyi b/stdlib/functools.pyi index de8e64893079..6f7df409313f 100644 --- a/stdlib/functools.pyi +++ b/stdlib/functools.pyi @@ -3,7 +3,7 @@ import types from _typeshed import IdentityFunction, 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 @@ -32,6 +32,7 @@ _AnyCallable: TypeAlias = Callable[..., object] _T = TypeVar("_T") _S = TypeVar("_S") +_P = ParamSpec("_P") @overload def reduce(function: Callable[[_T, _S], _T], sequence: Iterable[_S], initial: _T) -> _T: ... @@ -45,22 +46,22 @@ class _CacheInfo(NamedTuple): currsize: int @final -class _lru_cache_wrapper(Generic[_T]): - __wrapped__: Callable[..., _T] +class _lru_cache_wrapper(Generic[_P, _T]): + __wrapped__: Callable[_P, _T] def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ... 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, _T]: ... + def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[_P, _T]: ... 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, _T]], _lru_cache_wrapper[_P, _T]]: ... @overload - def lru_cache(maxsize: Callable[..., _T], typed: bool = False) -> _lru_cache_wrapper[_T]: ... + def lru_cache(maxsize: Callable[_P, _T], typed: bool = False) -> _lru_cache_wrapper[_P, _T]: ... 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, _T]], _lru_cache_wrapper[_P, _T]]: ... WRAPPER_ASSIGNMENTS: tuple[ Literal["__module__"], Literal["__name__"], Literal["__qualname__"], Literal["__doc__"], Literal["__annotations__"] @@ -152,7 +153,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, _T]) -> _lru_cache_wrapper[_P, _T]: ... def _make_key( args: tuple[Hashable, ...], From cded62ef54863906e70d20182840ee7fbfde886d Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Mon, 20 Feb 2023 14:35:32 +0100 Subject: [PATCH 2/6] Add missing types --- stdlib/functools.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/functools.pyi b/stdlib/functools.pyi index 6f7df409313f..884d3e3dfd32 100644 --- a/stdlib/functools.pyi +++ b/stdlib/functools.pyi @@ -48,7 +48,7 @@ class _CacheInfo(NamedTuple): @final class _lru_cache_wrapper(Generic[_P, _T]): __wrapped__: Callable[_P, _T] - def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ... + def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _T: ... def cache_info(self) -> _CacheInfo: ... def cache_clear(self) -> None: ... def __copy__(self) -> _lru_cache_wrapper[_P, _T]: ... From e53e9f57b68dbe8725e68557a5ec71b7ecbe83f2 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Mon, 20 Feb 2023 15:14:25 +0100 Subject: [PATCH 3/6] Fix instancemethods --- stdlib/functools.pyi | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/stdlib/functools.pyi b/stdlib/functools.pyi index e456ebe1c9cd..2aeeb2fdba90 100644 --- a/stdlib/functools.pyi +++ b/stdlib/functools.pyi @@ -3,7 +3,7 @@ import types from _typeshed import IdentityFunction, 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 @@ -53,6 +53,19 @@ class _lru_cache_wrapper(Generic[_P, _T]): def cache_clear(self) -> None: ... def __copy__(self) -> _lru_cache_wrapper[_P, _T]: ... def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[_P, _T]: ... + @overload + def __get__(self, instance: _S, owner: type[_S]) -> _lru_cache_wrapper_instance[_S, _P, _T]: ... + @overload + def __get__(self, instance: _S, owner: None) -> _lru_cache_wrapper[_P, _T]: ... + +@final +class _lru_cache_wrapper_instance(Generic[_S, _P, _T]): + __wrapped__: Callable[Concatenate[_S, _P], _T] + def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _T: ... + def cache_info(self) -> _CacheInfo: ... + def cache_clear(self) -> None: ... + def __copy__(self) -> _lru_cache_wrapper[Concatenate[_S, _P], _T]: ... + def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[Concatenate[_S, _P], _T]: ... if sys.version_info >= (3, 8): @overload From 740117a2185d7946ad80ed5530612287710f522f Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Mon, 20 Feb 2023 15:25:24 +0100 Subject: [PATCH 4/6] fix checher --- stdlib/functools.pyi | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/stdlib/functools.pyi b/stdlib/functools.pyi index 2aeeb2fdba90..1b086400d13d 100644 --- a/stdlib/functools.pyi +++ b/stdlib/functools.pyi @@ -53,19 +53,16 @@ class _lru_cache_wrapper(Generic[_P, _T]): def cache_clear(self) -> None: ... def __copy__(self) -> _lru_cache_wrapper[_P, _T]: ... def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[_P, _T]: ... - @overload - def __get__(self, instance: _S, owner: type[_S]) -> _lru_cache_wrapper_instance[_S, _P, _T]: ... - @overload - def __get__(self, instance: _S, owner: None) -> _lru_cache_wrapper[_P, _T]: ... - -@final -class _lru_cache_wrapper_instance(Generic[_S, _P, _T]): - __wrapped__: Callable[Concatenate[_S, _P], _T] - def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _T: ... - def cache_info(self) -> _CacheInfo: ... - def cache_clear(self) -> None: ... - def __copy__(self) -> _lru_cache_wrapper[Concatenate[_S, _P], _T]: ... - def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[Concatenate[_S, _P], _T]: ... + if sys.version_info >= (3, 8): + @overload + def __get__(self, instance: _S, owner: type[_S] = ...) -> Callable[Concatenate[_S, _P], _T]: ... + @overload + def __get__(self, instance: _S, owner: None = ...) -> _lru_cache_wrapper[_P, _T]: ... + else: + @overload + def __get__(self, instance: _S, owner: type[_S]) -> Callable[Concatenate[_S, _P], _T]: ... + @overload + def __get__(self, instance: _S, owner: None) -> _lru_cache_wrapper[_P, _T]: ... if sys.version_info >= (3, 8): @overload From efb901478c2782ff4286653daee1cf79d6d5df75 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Mon, 20 Feb 2023 15:36:26 +0100 Subject: [PATCH 5/6] fix --- stdlib/functools.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/functools.pyi b/stdlib/functools.pyi index 1b086400d13d..b56e7ac1bb71 100644 --- a/stdlib/functools.pyi +++ b/stdlib/functools.pyi @@ -55,14 +55,14 @@ class _lru_cache_wrapper(Generic[_P, _T]): def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[_P, _T]: ... if sys.version_info >= (3, 8): @overload - def __get__(self, instance: _S, owner: type[_S] = ...) -> Callable[Concatenate[_S, _P], _T]: ... + def __get__(self, __instance: None, __owner: type[_S]|None = ...) -> _lru_cache_wrapper[_P, _T]: ... @overload - def __get__(self, instance: _S, owner: None = ...) -> _lru_cache_wrapper[_P, _T]: ... + def __get__(self, __instance: _S, __owner: type[_S]|None = ...) -> Callable[Concatenate[_S, _P], _T]: ... else: @overload - def __get__(self, instance: _S, owner: type[_S]) -> Callable[Concatenate[_S, _P], _T]: ... + def __get__(self, __instance: None, __owner: type[_S]|None) -> _lru_cache_wrapper[_P, _T]: ... @overload - def __get__(self, instance: _S, owner: None) -> _lru_cache_wrapper[_P, _T]: ... + def __get__(self, __instance: _S, __owner: type[_S]|None) -> Callable[Concatenate[_S, _P], _T]: ... if sys.version_info >= (3, 8): @overload From 5dc302fb3b560b9aec96fe269d07d562b8c6ebfa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 Feb 2023 14:38:24 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/functools.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/functools.pyi b/stdlib/functools.pyi index b56e7ac1bb71..184968152527 100644 --- a/stdlib/functools.pyi +++ b/stdlib/functools.pyi @@ -55,14 +55,14 @@ class _lru_cache_wrapper(Generic[_P, _T]): def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[_P, _T]: ... if sys.version_info >= (3, 8): @overload - def __get__(self, __instance: None, __owner: type[_S]|None = ...) -> _lru_cache_wrapper[_P, _T]: ... + def __get__(self, __instance: None, __owner: type[_S] | None = ...) -> _lru_cache_wrapper[_P, _T]: ... @overload - def __get__(self, __instance: _S, __owner: type[_S]|None = ...) -> Callable[Concatenate[_S, _P], _T]: ... + def __get__(self, __instance: _S, __owner: type[_S] | None = ...) -> Callable[Concatenate[_S, _P], _T]: ... else: @overload - def __get__(self, __instance: None, __owner: type[_S]|None) -> _lru_cache_wrapper[_P, _T]: ... + def __get__(self, __instance: None, __owner: type[_S] | None) -> _lru_cache_wrapper[_P, _T]: ... @overload - def __get__(self, __instance: _S, __owner: type[_S]|None) -> Callable[Concatenate[_S, _P], _T]: ... + def __get__(self, __instance: _S, __owner: type[_S] | None) -> Callable[Concatenate[_S, _P], _T]: ... if sys.version_info >= (3, 8): @overload