Skip to content

Commit ea350e1

Browse files
committed
Add precise type annotations for cached decorator and helpers
1 parent 8dda4ea commit ea350e1

1 file changed

Lines changed: 46 additions & 5 deletions

File tree

stubs/cachetools/cachetools/__init__.pyi

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ from _typeshed import IdentityFunction, Unused
22
from collections.abc import Callable, Iterator, MutableMapping, Sequence
33
from contextlib import AbstractContextManager
44
from threading import Condition
5-
from typing import Any, TypeVar, overload
5+
from typing import Any, Generic, Literal, NamedTuple, TypeVar, overload
66
from typing_extensions import Self, deprecated
77

88
__all__ = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod")
9+
910
__version__: str
1011

1112
_KT = TypeVar("_KT")
1213
_VT = TypeVar("_VT")
1314
_T = TypeVar("_T")
15+
_R = TypeVar("_R")
1416

1517
class Cache(MutableMapping[_KT, _VT]):
1618
@overload
@@ -99,22 +101,61 @@ class TLRUCache(_TimedCache[_KT, _VT]):
99101
def ttu(self) -> Callable[[_KT, _VT, float], float]: ...
100102
def expire(self, time: float | None = None) -> list[tuple[_KT, _VT]]: ...
101103

104+
class _CacheInfo(NamedTuple):
105+
hits: int
106+
misses: int
107+
maxsize: int | None
108+
currsize: int
109+
110+
class _cached_wrapper(Generic[_R]):
111+
__wrapped__: Callable[..., _R]
112+
def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ...
113+
114+
class _cached_wrapper_info(_cached_wrapper[_R]):
115+
def cache_info(self) -> _CacheInfo: ...
116+
def cache_clear(self) -> None: ...
117+
102118
@overload
103119
def cached(
104120
cache: MutableMapping[_KT, Any] | None,
105121
key: Callable[..., _KT] = ...,
106122
lock: AbstractContextManager[Any] | None = None,
107123
condition: Condition | None = None,
108-
info: bool = False,
109-
) -> IdentityFunction: ...
124+
*,
125+
info: Literal[True],
126+
) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ...
127+
@overload
128+
def cached(
129+
cache: MutableMapping[_KT, Any] | None,
130+
key: Callable[..., _KT] = ...,
131+
lock: AbstractContextManager[Any] | None = None,
132+
condition: Condition | None = None,
133+
*,
134+
info: Literal[False] = False,
135+
) -> Callable[[Callable[..., _R]], _cached_wrapper[_R]]: ...
136+
@overload
137+
def cached( # без параметра info (по умолчанию False)
138+
cache: MutableMapping[_KT, Any] | None,
139+
key: Callable[..., _KT] = ...,
140+
lock: AbstractContextManager[Any] | None = None,
141+
condition: Condition | None = None,
142+
) -> Callable[[Callable[..., _R]], _cached_wrapper[_R]]: ...
110143
@overload
111144
@deprecated("Passing `info` as positional parameter is deprecated.")
112145
def cached(
113146
cache: MutableMapping[_KT, Any] | None,
114147
key: Callable[..., _KT] = ...,
115148
lock: AbstractContextManager[Any] | None = None,
116-
condition: bool | None = None,
117-
) -> IdentityFunction: ...
149+
condition: Literal[True] = ...,
150+
) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ...
151+
@overload
152+
@deprecated("Passing `info` as positional parameter is deprecated.")
153+
def cached(
154+
cache: MutableMapping[_KT, Any] | None,
155+
key: Callable[..., _KT] = ...,
156+
lock: AbstractContextManager[Any] | None = None,
157+
condition: Literal[False] | None = ...,
158+
) -> Callable[[Callable[..., _R]], _cached_wrapper[_R]]: ...
118159
def cachedmethod(
119160
cache: Callable[[Any], MutableMapping[_KT, Any] | None],
120161
key: Callable[..., _KT] = ...,

0 commit comments

Comments
 (0)