diff --git a/stubs/cachetools/cachetools/__init__.pyi b/stubs/cachetools/cachetools/__init__.pyi index 47db5e279956..8adfb544c640 100644 --- a/stubs/cachetools/cachetools/__init__.pyi +++ b/stubs/cachetools/cachetools/__init__.pyi @@ -2,7 +2,7 @@ from _typeshed import IdentityFunction, Unused from collections.abc import Callable, Iterator, MutableMapping, Sequence from contextlib import AbstractContextManager from threading import Condition -from typing import Any, TypeVar, overload +from typing import Any, Generic, Literal, NamedTuple, TypeVar, overload from typing_extensions import Self, deprecated __all__ = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod") @@ -11,6 +11,7 @@ __version__: str _KT = TypeVar("_KT") _VT = TypeVar("_VT") _T = TypeVar("_T") +_R = TypeVar("_R") class Cache(MutableMapping[_KT, _VT]): @overload @@ -99,22 +100,52 @@ class TLRUCache(_TimedCache[_KT, _VT]): def ttu(self) -> Callable[[_KT, _VT, float], float]: ... def expire(self, time: float | None = None) -> list[tuple[_KT, _VT]]: ... +class _CacheInfo(NamedTuple): + hits: int + misses: int + maxsize: int | None + currsize: int + +class _cached_wrapper(Generic[_R]): + __wrapped__: Callable[..., _R] + def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ... + +class _cached_wrapper_info(_cached_wrapper[_R]): + def cache_info(self) -> _CacheInfo: ... + def cache_clear(self) -> None: ... + @overload def cached( cache: MutableMapping[_KT, Any] | None, key: Callable[..., _KT] = ..., lock: AbstractContextManager[Any] | None = None, condition: Condition | None = None, - info: bool = False, -) -> IdentityFunction: ... + info: Literal[True] = ..., +) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ... +@overload +def cached( + cache: MutableMapping[_KT, Any] | None, + key: Callable[..., _KT] = ..., + lock: AbstractContextManager[Any] | None = None, + condition: Condition | None = None, + info: Literal[False] = ..., +) -> Callable[[Callable[..., _R]], _cached_wrapper[_R]]: ... @overload @deprecated("Passing `info` as positional parameter is deprecated.") def cached( cache: MutableMapping[_KT, Any] | None, key: Callable[..., _KT] = ..., lock: AbstractContextManager[Any] | None = None, - condition: bool | None = None, -) -> IdentityFunction: ... + condition: Literal[True] = ..., +) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ... +@overload +@deprecated("Passing `info` as positional parameter is deprecated.") +def cached( + cache: MutableMapping[_KT, Any] | None, + key: Callable[..., _KT] = ..., + lock: AbstractContextManager[Any] | None = None, + condition: Literal[False] | None = ..., +) -> Callable[[Callable[..., _R]], _cached_wrapper[_R]]: ... def cachedmethod( cache: Callable[[Any], MutableMapping[_KT, Any] | None], key: Callable[..., _KT] = ..., diff --git a/stubs/cachetools/cachetools/func.pyi b/stubs/cachetools/cachetools/func.pyi index 8608a1060e43..0480334ffe19 100644 --- a/stubs/cachetools/cachetools/func.pyi +++ b/stubs/cachetools/cachetools/func.pyi @@ -1,16 +1,51 @@ -from _typeshed import IdentityFunction from collections.abc import Callable, Sequence -from typing import TypeVar +from typing import Any, Final, Generic, NamedTuple, TypeVar, overload + +__all__: Final = ("fifo_cache", "lfu_cache", "lru_cache", "rr_cache", "ttl_cache") -__all__ = ("fifo_cache", "lfu_cache", "lru_cache", "rr_cache", "ttl_cache") _T = TypeVar("_T") +_R = TypeVar("_R") + +class _CacheInfo(NamedTuple): + hits: int + misses: int + maxsize: int | None + currsize: int + +class _cachetools_cache_wrapper(Generic[_R]): + __wrapped__: Callable[..., _R] + def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ... + def cache_info(self) -> _CacheInfo: ... + def cache_clear(self) -> None: ... + def cache_parameters(self) -> dict[str, Any]: ... -def fifo_cache(maxsize: float | None = 128, typed: bool = False) -> IdentityFunction: ... -def lfu_cache(maxsize: float | None = 128, typed: bool = False) -> IdentityFunction: ... -def lru_cache(maxsize: float | None = 128, typed: bool = False) -> IdentityFunction: ... +@overload +def fifo_cache( + maxsize: int | None = 128, typed: bool = False +) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ... +@overload +def fifo_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ... +@overload +def lfu_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ... +@overload +def lfu_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ... +@overload +def lru_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ... +@overload +def lru_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ... +@overload def rr_cache( - maxsize: float | None = 128, choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False -) -> IdentityFunction: ... + maxsize: int | None = 128, choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False +) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ... +@overload +def rr_cache( + maxsize: Callable[..., _R], choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False +) -> _cachetools_cache_wrapper[_R]: ... +@overload +def ttl_cache( + maxsize: int | None = 128, ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False +) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ... +@overload def ttl_cache( - maxsize: float | None = 128, ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False -) -> IdentityFunction: ... + maxsize: Callable[..., _R], ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False +) -> _cachetools_cache_wrapper[_R]: ... diff --git a/stubs/cachetools/cachetools/keys.pyi b/stubs/cachetools/cachetools/keys.pyi index 2f16e47edbf8..be1c7903c9c0 100644 --- a/stubs/cachetools/cachetools/keys.pyi +++ b/stubs/cachetools/cachetools/keys.pyi @@ -4,6 +4,6 @@ from collections.abc import Hashable __all__ = ("hashkey", "methodkey", "typedkey", "typedmethodkey") def hashkey(*args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ... -def methodkey(self: Unused, *args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ... +def methodkey(self: Unused, /, *args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ... def typedkey(*args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ... -def typedmethodkey(self: Unused, *args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ... +def typedmethodkey(self: Unused, /, *args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ...