Skip to content

Add type signatures to lru_cache using overloads (not ready to merge) #13033

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 2 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
44 changes: 35 additions & 9 deletions stdlib/functools.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sys
import types
from _typeshed import SupportsAllComparisons, SupportsItems
from collections.abc import Callable, Hashable, Iterable, Sequence, Sized
from typing import Any, Generic, Literal, NamedTuple, TypedDict, TypeVar, final, overload
from typing import Any, Generic, Literal, NamedTuple, Protocol, TypedDict, TypeVar, final, overload
from typing_extensions import ParamSpec, Self, TypeAlias

if sys.version_info >= (3, 9):
Expand Down Expand Up @@ -30,6 +30,8 @@ if sys.version_info >= (3, 9):
_T = TypeVar("_T")
_T_co = TypeVar("_T_co", covariant=True)
_S = TypeVar("_S")
_R = TypeVar("_R")
_P = ParamSpec("_P")
_PWrapped = ParamSpec("_PWrapped")
_RWrapped = TypeVar("_RWrapped")
_PWrapper = ParamSpec("_PWrapper")
Expand All @@ -51,22 +53,46 @@ if sys.version_info >= (3, 9):
maxsize: int
typed: bool

class _Function(Protocol[_P, _R]): # type: ignore[misc] # pyright: ignore reportInvalidTypeVarUse
def __call__(self, *args: _P.args, **kwds: _P.kwargs) -> _R: ...

class _Method(Protocol[_T, _P, _R]): # type: ignore[misc] # pyright: ignore reportInvalidTypeVarUse
def __call__(_self, self: _T, *args: _P.args, **kwds: _P.kwargs) -> _R: ...

class _Classmethod(Protocol[_T, _P, _R]): # type: ignore[misc] # pyright: ignore reportInvalidTypeVarUse
def __call__(_self, cls: type[_T], *args: _P.args, **kwds: _P.kwargs) -> _R: ...
Comment on lines +56 to +63
Copy link
Author

@alwaysmpe alwaysmpe Nov 18, 2024

Choose a reason for hiding this comment

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

I think combine these and use Concatenate instead, avoid needing to explicitly name a parameter, partially fix issue mentioned by @Daverball

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that's pretty much what the closest to working earlier approach did, yes. But it still had its edge cases where it didn't work unfortunately. Maybe things are a little bit more forgiving now.

If I'm not mistaken what you're doing in addition to earlier approaches is to always accept both signatures for methods/classmethods (i.e. the one where you have to explicitly provide self/cls and the one where you don't), so you give up some type safety on instance.method() vs. Cls.method() since it would not be able to detect that you're missing a required parameter.

That might be a reasonable trade-off for the overall better type safety, although I fear that language servers will not know what to do with these overloads, so instead of no parameter hints you may get confusing/difficult to read hints, which could be seen as a regression in ergonomics.


_CacheFn: TypeAlias = _lru_cache_wrapper[_Function[_P, _R]]
_CacheMethod: TypeAlias = _lru_cache_wrapper[_Method[_T, _P, _R]]
_CacheClassmethod: TypeAlias = _lru_cache_wrapper[_Classmethod[_T, _P, _R]]
_C = TypeVar("_C", bound=Callable[..., Any])

@final
class _lru_cache_wrapper(Generic[_T]):
__wrapped__: Callable[..., _T]
def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ...
class _lru_cache_wrapper(Generic[_C]):
__wrapped__: _C

@overload
def __call__(_self: _CacheMethod[_T, _P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
@overload
def __call__(_self: _CacheMethod[_T, _P, _R], self: _T, *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
@overload
def __call__(_self: _CacheClassmethod[_T, _P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
@overload
def __call__(_self: _CacheClassmethod[_T, _P, _R], cls: type[_T], *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
@overload
def __call__(_self: _CacheFn[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
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) -> Self: ...
def __deepcopy__(self, memo: Any, /) -> Self: ...

@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[[_C], _lru_cache_wrapper[_C]]: ...
@overload
def lru_cache(maxsize: Callable[..., _T], typed: bool = False) -> _lru_cache_wrapper[_T]: ...
def lru_cache(maxsize: _C, typed: bool = False) -> _lru_cache_wrapper[_C]: ...

if sys.version_info >= (3, 12):
WRAPPER_ASSIGNMENTS: tuple[
Expand Down Expand Up @@ -199,7 +225,7 @@ class cached_property(Generic[_T_co]):
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: _C, /) -> _lru_cache_wrapper[_C]: ...

def _make_key(
args: tuple[Hashable, ...],
Expand Down