Skip to content
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

functools: Add typing for decorated function arguments in cache API [take 2] #10523

Closed
wants to merge 8 commits into from
32 changes: 22 additions & 10 deletions stdlib/functools.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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, TypedDict, final
from typing_extensions import Concatenate, Literal, ParamSpec, Self, TypeAlias, TypedDict, final

if sys.version_info >= (3, 9):
from types import GenericAlias
Expand All @@ -30,6 +30,8 @@

_T = TypeVar("_T")
_S = TypeVar("_S")
_P = ParamSpec("_P")

_PWrapped = ParamSpec("_PWrapped")
_RWrapped = TypeVar("_RWrapped")
_PWrapper = ParamSpec("_PWrapper")
Expand All @@ -52,25 +54,35 @@
typed: bool

@final
class _lru_cache_wrapper(Generic[_T]):
__wrapped__: Callable[..., _T]
def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ...
class _lru_cache_wrapper(Generic[_P, _T]):
__wrapped__: Callable[_P, _T]
def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _T: ...
def cache_info(self) -> _CacheInfo: ...
def cache_clear(self) -> None: ...
if sys.version_info >= (3, 9):
def cache_parameters(self) -> _CacheParameters: ...

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 __get__(self, __instance: None, __owner: type[_S] | None = ...) -> _lru_cache_wrapper[_P, _T]: ...

Check failure on line 69 in stdlib/functools.pyi

View workflow job for this annotation

GitHub Actions / Test typeshed with pyright (Linux, 3.11)

TypeVar "_S" appears only once in generic function signature (reportInvalidTypeVarUse)
@overload
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]: ...
@overload
def __get__(self, __instance: _S, __owner: type[_S] | None) -> Callable[Concatenate[_S, _P], _T]: ...
Comment on lines +65 to +76
Copy link
Contributor

@Daverball Daverball Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 __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]: ...
else:
@overload
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 __copy__(self) -> _lru_cache_wrapper[_P, _T]: ...
def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[_P, _T]: ...
if sys.version_info >= (3, 8):
def __get__(self, __instance: object | None, __owner: type[object] | None = ...) -> Self: ...
else:
def __get__(self, __instance: object | None, __owner: type[object] | None) -> Self: ...

I think this should always return itself, since you want the additional properties on the wrapper to be available regardless of whether you're accessing the wrapper from the instance or a class. It remaining callable with the same parameters is guaranteed by the __call__ method.


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]]: ...

if sys.version_info >= (3, 12):
WRAPPER_ASSIGNMENTS: tuple[
Expand Down Expand Up @@ -208,7 +220,7 @@
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, ...],
Expand Down
Loading