cachetools: precise typing for decorators and cached(); expose cache_info/cache_clear and fix keys signatures (#14770)

This commit is contained in:
Shamil
2025-10-21 13:00:43 +02:00
committed by GitHub
parent 4c4f999717
commit fa0e5fc066
4 changed files with 187 additions and 17 deletions
+36 -5
View File
@@ -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] = ...,
+45 -10
View File
@@ -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")
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: ...
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]: ...
@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: float | None = 128, ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False
) -> IdentityFunction: ...
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: Callable[..., _R], ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False
) -> _cachetools_cache_wrapper[_R]: ...
+2 -2
View File
@@ -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, ...]: ...