Skip to content

functools: Add typing for decorated function arguments in cache API #9768

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

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions stdlib/functools.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 Concatenate, Literal, ParamSpec, Self, TypeAlias, final

if sys.version_info >= (3, 9):
from types import GenericAlias
Expand Down Expand Up @@ -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: ...
Expand All @@ -45,22 +46,32 @@ 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, _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: ...
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]: ...
@overload
def __get__(self, __instance: _S, __owner: type[_S] | None = ...) -> Callable[Concatenate[_S, _P], _T]: ...

Choose a reason for hiding this comment

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

It seems you have this backward. I think it should be this:

Suggested change
def __get__(self, __instance: _S, __owner: type[_S] | None = ...) -> Callable[Concatenate[_S, _P], _T]: ...
def __get__(self: _lru_cache_wrapper[Concatenate[_S, _Q], _T], __instance: _S, __owner: type[_S] | None = ...) -> Callable[_Q, _T]: ...

Where _Q = ParamSpec("_Q"). You'd also want to change the one for 3.8 below.

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

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__"]
Expand Down Expand Up @@ -154,7 +165,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, ...],
Expand Down