Skip to content

Commit be05cbd

Browse files
committed
[cachetools] Update to 7.0.* (#15357)
1 parent eec9fe9 commit be05cbd

File tree

5 files changed

+167
-89
lines changed

5 files changed

+167
-89
lines changed

stubs/cachetools/@tests/stubtest_allowlist.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ cachetools.LFUCache.__setitem__
99
cachetools.LRUCache.__delitem__
1010
cachetools.LRUCache.__getitem__
1111
cachetools.LRUCache.__setitem__
12+
cachetools.RRCache.__delitem__
13+
cachetools.RRCache.__getitem__
14+
cachetools.RRCache.__setitem__
1215
cachetools.TLRUCache.__delitem__
1316
cachetools.TLRUCache.__getitem__
1417
cachetools.TLRUCache.__setitem__

stubs/cachetools/METADATA.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version = "6.2.*"
1+
version = "7.0.*"
22
upstream-repository = "https://github.com/tkem/cachetools"
Lines changed: 126 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,49 @@
1-
from _typeshed import IdentityFunction, Unused
1+
import random
22
from collections.abc import Callable, Iterator, MutableMapping, Sequence
33
from contextlib import AbstractContextManager
4-
from threading import Condition
5-
from typing import Any, Generic, Literal, NamedTuple, TypeVar, overload, type_check_only
6-
from typing_extensions import Self, deprecated
4+
from typing import (
5+
Any,
6+
Final,
7+
Generic,
8+
Literal,
9+
NamedTuple,
10+
Protocol,
11+
TypeVar,
12+
overload,
13+
type_check_only,
14+
)
715

8-
__all__ = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod")
16+
__all__: Final = (
17+
"Cache",
18+
"FIFOCache",
19+
"LFUCache",
20+
"LRUCache",
21+
"RRCache",
22+
"TLRUCache",
23+
"TTLCache",
24+
"cached",
25+
"cachedmethod",
26+
)
927
__version__: str
1028

1129
_KT = TypeVar("_KT")
1230
_VT = TypeVar("_VT")
31+
_TT = TypeVar("_TT")
1332
_T = TypeVar("_T")
1433
_R = TypeVar("_R")
34+
_KT2 = TypeVar("_KT2")
35+
_VT2 = TypeVar("_VT2")
1536

1637
class Cache(MutableMapping[_KT, _VT]):
17-
@overload
18-
def __init__(self, maxsize: float, getsizeof: Callable[[_VT], float]) -> None: ...
19-
@overload
20-
def __init__(self, maxsize: float, getsizeof: None = None) -> None: ...
38+
def __init__(
39+
self, maxsize: float, getsizeof: Callable[[_VT], float] | None = None
40+
): ...
2141
def __getitem__(self, key: _KT) -> _VT: ...
2242
def __setitem__(self, key: _KT, value: _VT) -> None: ...
2343
def __delitem__(self, key: _KT) -> None: ...
2444
def __missing__(self, key: _KT) -> _VT: ...
2545
def __iter__(self) -> Iterator[_KT]: ...
2646
def __len__(self) -> int: ...
27-
@overload
28-
def pop(self, key: _KT) -> _VT: ...
29-
@overload
30-
def pop(self, key: _KT, default: _VT | _T) -> _VT | _T: ...
3147
def setdefault(self, key: _KT, default: _VT | None = None) -> _VT: ...
3248
@property
3349
def maxsize(self) -> float: ...
@@ -41,116 +57,150 @@ class LFUCache(Cache[_KT, _VT]): ...
4157
class LRUCache(Cache[_KT, _VT]): ...
4258

4359
class RRCache(Cache[_KT, _VT]):
44-
@overload
45-
def __init__(self, maxsize: float, choice: None = None, getsizeof: None = None) -> None: ...
46-
@overload
47-
def __init__(self, maxsize: float, *, getsizeof: Callable[[_VT], float]) -> None: ...
48-
@overload
49-
def __init__(self, maxsize: float, choice: None, getsizeof: Callable[[_VT], float]) -> None: ...
50-
@overload
51-
def __init__(self, maxsize: float, choice: Callable[[Sequence[_KT]], _KT], getsizeof: None = None) -> None: ...
52-
@overload
53-
def __init__(self, maxsize: float, choice: Callable[[Sequence[_KT]], _KT], getsizeof: Callable[[_VT], float]) -> None: ...
60+
def __init__(
61+
self,
62+
maxsize: float,
63+
choice: Callable[[Sequence[_KT]], _KT] = random.choice,
64+
getsizeof: Callable[[_VT], float] | None = None,
65+
) -> None: ...
5466
@property
5567
def choice(self) -> Callable[[Sequence[_KT]], _KT]: ...
56-
def __setitem__(self, key: _KT, value: _VT, cache_setitem: Callable[[Self, _KT, _VT], None] = ...) -> None: ...
57-
def __delitem__(self, key: _KT, cache_delitem: Callable[[Self, _KT], None] = ...) -> None: ...
5868

59-
class _TimedCache(Cache[_KT, _VT]):
60-
@overload
61-
def __init__(self, maxsize: float, timer: Callable[[], float] = ..., getsizeof: None = None) -> None: ...
62-
@overload
63-
def __init__(self, maxsize: float, timer: Callable[[], float], getsizeof: Callable[[_VT], float]) -> None: ...
64-
@overload
65-
def __init__(self, maxsize: float, timer: Callable[[], float] = ..., *, getsizeof: Callable[[_VT], float]) -> None: ...
66-
@property
67-
def currsize(self) -> float: ...
69+
class _TimedCache(Cache[_KT, _VT], Generic[_KT, _VT, _TT]):
70+
def __init__(
71+
self,
72+
maxsize: float,
73+
timer: Callable[[], _TT],
74+
getsizeof: Callable[[_VT], float] | None = None,
75+
): ...
6876

69-
class _Timer:
70-
def __init__(self, timer: Callable[[], float]) -> None: ...
71-
def __call__(self) -> float: ...
72-
def __enter__(self) -> float: ...
73-
def __exit__(self, *exc: Unused) -> None: ...
77+
class _Timer(AbstractContextManager[_T]):
78+
def __init__(self, timer: Callable[[], _T]) -> None: ...
79+
def __call__(self) -> _T: ...
80+
def __enter__(self) -> _T: ...
81+
def __exit__(self, *exc: object) -> None: ...
82+
def __getattr__(self, name: str) -> Any: ...
7483

7584
@property
76-
def timer(self) -> _Timer: ...
85+
def timer(self) -> _Timer[_TT]: ...
7786

78-
class TTLCache(_TimedCache[_KT, _VT]):
79-
@overload
80-
def __init__(self, maxsize: float, ttl: float, timer: Callable[[], float] = ..., getsizeof: None = None) -> None: ...
87+
class TTLCache(_TimedCache[_KT, _VT, _TT]):
8188
@overload
82-
def __init__(self, maxsize: float, ttl: float, timer: Callable[[], float], getsizeof: Callable[[_VT], float]) -> None: ...
89+
def __init__(
90+
self: TTLCache[_KT2, _VT2, float],
91+
maxsize: float,
92+
ttl: float,
93+
*,
94+
getsizeof: Callable[[_VT2], float] | None = None,
95+
): ...
8396
@overload
8497
def __init__(
85-
self, maxsize: float, ttl: float, timer: Callable[[], float] = ..., *, getsizeof: Callable[[_VT], float]
86-
) -> None: ...
98+
self,
99+
maxsize: float,
100+
ttl: Any, # FIXME: must be "addable" to _TT
101+
timer: Callable[[], _TT],
102+
getsizeof: Callable[[_VT], float] | None = None,
103+
): ...
87104
@property
88-
def ttl(self) -> float: ...
89-
def expire(self, time: float | None = None) -> list[tuple[_KT, _VT]]: ...
105+
def ttl(self) -> Any: ...
106+
def expire(self, time: _TT | None = None) -> list[tuple[_KT, _VT]]: ...
90107

91-
class TLRUCache(_TimedCache[_KT, _VT]):
108+
class TLRUCache(_TimedCache[_KT, _VT, _TT]):
109+
@overload
110+
def __init__(
111+
self: TLRUCache[_KT2, _VT2, float],
112+
maxsize: float,
113+
ttu: Callable[[_KT2, _VT2, float], float],
114+
*,
115+
getsizeof: Callable[[_VT2], float] | None = None,
116+
): ...
117+
@overload
92118
def __init__(
93119
self,
94120
maxsize: float,
95-
ttu: Callable[[_KT, _VT, float], float],
96-
timer: Callable[[], float] = ...,
121+
ttu: Callable[[_KT, _VT, _TT], _TT],
122+
timer: Callable[[], _TT],
97123
getsizeof: Callable[[_VT], float] | None = None,
98-
) -> None: ...
124+
): ...
99125
@property
100-
def ttu(self) -> Callable[[_KT, _VT, float], float]: ...
101-
def expire(self, time: float | None = None) -> list[tuple[_KT, _VT]]: ...
126+
def ttu(self) -> Callable[[_KT, _VT, _TT], _TT]: ...
127+
def expire(self, time: _TT | None = None) -> list[tuple[_KT, _VT]]: ...
102128

103129
class _CacheInfo(NamedTuple):
104130
hits: int
105131
misses: int
106-
maxsize: int | None
107-
currsize: int
132+
maxsize: float | None
133+
currsize: float
134+
135+
@type_check_only
136+
class _AbstractCondition(AbstractContextManager[Any], Protocol):
137+
# implementation an unit tests do not use plain wait() and notify()
138+
def wait_for(
139+
self, predicate: Callable[[], _T], timeout: float | None = None
140+
) -> _T: ...
141+
def notify_all(self) -> None: ...
108142

109143
@type_check_only
110144
class _cached_wrapper(Generic[_R]):
111145
__wrapped__: Callable[..., _R]
146+
__name__: str
147+
__doc__: str | None
148+
cache: MutableMapping[Any, Any] | None
149+
cache_key: Callable[..., Any] = ...
150+
cache_lock: AbstractContextManager[Any] | None = None
151+
cache_condition: _AbstractCondition | None = None
112152
def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ...
153+
def cache_clear(self) -> None: ...
113154

114155
@type_check_only
115156
class _cached_wrapper_info(_cached_wrapper[_R]):
116157
def cache_info(self) -> _CacheInfo: ...
117-
def cache_clear(self) -> None: ...
118158

119159
@overload
120160
def cached(
121161
cache: MutableMapping[_KT, Any] | None,
122162
key: Callable[..., _KT] = ...,
123163
lock: AbstractContextManager[Any] | None = None,
124-
condition: Condition | None = None,
164+
condition: _AbstractCondition | None = None,
125165
info: Literal[True] = ...,
126166
) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ...
127167
@overload
128168
def cached(
129169
cache: MutableMapping[_KT, Any] | None,
130170
key: Callable[..., _KT] = ...,
131171
lock: AbstractContextManager[Any] | None = None,
132-
condition: Condition | None = None,
133-
info: Literal[False] = ...,
172+
condition: _AbstractCondition | None = None,
173+
info: Literal[False] = False,
134174
) -> Callable[[Callable[..., _R]], _cached_wrapper[_R]]: ...
175+
@type_check_only
176+
class _cachedmethod_wrapper(Generic[_R]):
177+
__wrapped__: Callable[..., _R]
178+
__name__: str
179+
__doc__: str | None
180+
cache: MutableMapping[Any, Any] | None
181+
cache_key: Callable[..., Any] = ...
182+
cache_lock: AbstractContextManager[Any] | None = None
183+
cache_condition: _AbstractCondition | None = None
184+
def __call__(self, obj, /, *args: Any, **kwargs: Any) -> _R: ...
185+
def cache_clear(self) -> None: ...
186+
187+
@type_check_only
188+
class _cachedmethod_wrapper_info(_cachedmethod_wrapper[_R]):
189+
def cache_info(self) -> _CacheInfo: ...
190+
135191
@overload
136-
@deprecated("Passing `info` as positional parameter is deprecated.")
137-
def cached(
138-
cache: MutableMapping[_KT, Any] | None,
192+
def cachedmethod(
193+
cache: Callable[[Any], MutableMapping[_KT, Any]],
139194
key: Callable[..., _KT] = ...,
140-
lock: AbstractContextManager[Any] | None = None,
141-
condition: Literal[True] = ...,
142-
) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ...
195+
lock: Callable[[Any], AbstractContextManager[Any]] | None = None,
196+
condition: Callable[[Any], _AbstractCondition] | None = None,
197+
info: Literal[True] = ...,
198+
) -> Callable[[Callable[..., _R]], _cachedmethod_wrapper_info[_R]]: ...
143199
@overload
144-
@deprecated("Passing `info` as positional parameter is deprecated.")
145-
def cached(
146-
cache: MutableMapping[_KT, Any] | None,
147-
key: Callable[..., _KT] = ...,
148-
lock: AbstractContextManager[Any] | None = None,
149-
condition: Literal[False] | None = ...,
150-
) -> Callable[[Callable[..., _R]], _cached_wrapper[_R]]: ...
151200
def cachedmethod(
152-
cache: Callable[[Any], MutableMapping[_KT, Any] | None],
201+
cache: Callable[[Any], MutableMapping[_KT, Any]],
153202
key: Callable[..., _KT] = ...,
154203
lock: Callable[[Any], AbstractContextManager[Any]] | None = None,
155-
condition: Condition | None = None,
156-
) -> IdentityFunction: ...
204+
condition: Callable[[Any], _AbstractCondition] | None = None,
205+
info: Literal[False] = False,
206+
) -> Callable[[Callable[..., _R]], _cachedmethod_wrapper[_R]]: ...

stubs/cachetools/cachetools/func.pyi

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,48 @@ def fifo_cache(
2121
maxsize: int | None = 128, typed: bool = False
2222
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
2323
@overload
24-
def fifo_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ...
24+
def fifo_cache(
25+
maxsize: Callable[..., _R], typed: bool = False
26+
) -> _cachetools_cache_wrapper[_R]: ...
2527
@overload
26-
def lfu_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
28+
def lfu_cache(
29+
maxsize: int | None = 128, typed: bool = False
30+
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
2731
@overload
28-
def lfu_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ...
32+
def lfu_cache(
33+
maxsize: Callable[..., _R], typed: bool = False
34+
) -> _cachetools_cache_wrapper[_R]: ...
2935
@overload
30-
def lru_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
36+
def lru_cache(
37+
maxsize: int | None = 128, typed: bool = False
38+
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
3139
@overload
32-
def lru_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ...
40+
def lru_cache(
41+
maxsize: Callable[..., _R], typed: bool = False
42+
) -> _cachetools_cache_wrapper[_R]: ...
3343
@overload
3444
def rr_cache(
35-
maxsize: int | None = 128, choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False
45+
maxsize: int | None = 128,
46+
choice: Callable[[Sequence[_T]], _T] = ...,
47+
typed: bool = False,
3648
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
3749
@overload
3850
def rr_cache(
39-
maxsize: Callable[..., _R], choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False
51+
maxsize: Callable[..., _R],
52+
choice: Callable[[Sequence[_T]], _T] = ...,
53+
typed: bool = False,
4054
) -> _cachetools_cache_wrapper[_R]: ...
4155
@overload
4256
def ttl_cache(
43-
maxsize: int | None = 128, ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False
57+
maxsize: int | None = 128,
58+
ttl: Any = 600,
59+
timer: Callable[[], _T] = ...,
60+
typed: bool = False,
4461
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
4562
@overload
4663
def ttl_cache(
47-
maxsize: Callable[..., _R], ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False
64+
maxsize: Callable[..., _R],
65+
ttl: Any = 600,
66+
timer: Callable[[], _T] = ...,
67+
typed: bool = False,
4868
) -> _cachetools_cache_wrapper[_R]: ...
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
from _typeshed import Unused
22
from collections.abc import Hashable
3+
from typing import Final
34

4-
__all__ = ("hashkey", "methodkey", "typedkey", "typedmethodkey")
5+
__all__: Final = ("hashkey", "methodkey", "typedkey", "typedmethodkey")
56

67
def hashkey(*args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ...
7-
def methodkey(self: Unused, /, *args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ...
8+
def methodkey(
9+
self: Unused, /, *args: Hashable, **kwargs: Hashable
10+
) -> tuple[Hashable, ...]: ...
811
def typedkey(*args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ...
9-
def typedmethodkey(self: Unused, /, *args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ...
12+
def typedmethodkey(
13+
self: Unused, /, *args: Hashable, **kwargs: Hashable
14+
) -> tuple[Hashable, ...]: ...

0 commit comments

Comments
 (0)