Skip to content

Commit 6cc21db

Browse files
committed
[cachetools] Update to 7.0.* (#15357)
1 parent a5dd996 commit 6cc21db

File tree

6 files changed

+165
-84
lines changed

6 files changed

+165
-84
lines changed

stubs/cachetools/@tests/stubtest_allowlist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ cachetools.LFUCache.__setitem__
99
cachetools.LRUCache.__delitem__
1010
cachetools.LRUCache.__getitem__
1111
cachetools.LRUCache.__setitem__
12+
cachetools.RRCache.__delitem__
13+
cachetools.RRCache.__setitem__
1214
cachetools.TLRUCache.__delitem__
1315
cachetools.TLRUCache.__getitem__
1416
cachetools.TLRUCache.__setitem__

stubs/cachetools/@tests/test_cases/check_cachetools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def ttl_func(x: int) -> int:
7777
assert_type(lfu_func(1), int)
7878
assert_type(rr_func(1), int)
7979
assert_type(ttl_func(1), int)
80-
assert_type(fifo_func.cache_info().currsize, int)
80+
assert_type(fifo_func.cache_info().currsize, float)
8181
assert_type(lfu_func.cache_parameters(), dict[str, Any])
8282

8383

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: 124 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
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", default=float)
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+
) -> None: ...
2141
def __getitem__(self, key: _KT) -> _VT: ...
2242
def __setitem__(self, key: _KT, value: _VT) -> None: ...
2343
def __delitem__(self, key: _KT) -> None: ...
@@ -41,116 +61,150 @@ class LFUCache(Cache[_KT, _VT]): ...
4161
class LRUCache(Cache[_KT, _VT]): ...
4262

4363
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: ...
64+
def __init__(
65+
self,
66+
maxsize: float,
67+
choice: Callable[[Sequence[_KT]], _KT] = random.choice,
68+
getsizeof: Callable[[_VT], float] | None = None,
69+
) -> None: ...
5470
@property
5571
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: ...
5872

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: ...
73+
class _TimedCache(Cache[_KT, _VT], Generic[_KT, _VT, _TT]):
74+
def __init__(
75+
self,
76+
maxsize: float,
77+
timer: Callable[[], _TT],
78+
getsizeof: Callable[[_VT], float] | None = None,
79+
) -> None: ...
6880

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: ...
81+
class _Timer(AbstractContextManager[_T]):
82+
def __init__(self, timer: Callable[[], _T]) -> None: ...
83+
def __call__(self) -> _T: ...
84+
def __enter__(self) -> _T: ...
85+
def __exit__(self, *exc: object) -> None: ...
86+
def __getattr__(self, name: str) -> Any: ...
7487

7588
@property
76-
def timer(self) -> _Timer: ...
89+
def timer(self) -> _Timer[_TT]: ...
7790

78-
class TTLCache(_TimedCache[_KT, _VT]):
79-
@overload
80-
def __init__(self, maxsize: float, ttl: float, timer: Callable[[], float] = ..., getsizeof: None = None) -> None: ...
91+
class TTLCache(_TimedCache[_KT, _VT, _TT]):
8192
@overload
82-
def __init__(self, maxsize: float, ttl: float, timer: Callable[[], float], getsizeof: Callable[[_VT], float]) -> None: ...
93+
def __init__(
94+
self: TTLCache[_KT2, _VT2, float],
95+
maxsize: float,
96+
ttl: float,
97+
*,
98+
getsizeof: Callable[[_VT2], float] | None = None,
99+
) -> None: ...
83100
@overload
84101
def __init__(
85-
self, maxsize: float, ttl: float, timer: Callable[[], float] = ..., *, getsizeof: Callable[[_VT], float]
102+
self,
103+
maxsize: float,
104+
ttl: Any, # FIXME: must be "addable" to _TT
105+
timer: Callable[[], _TT],
106+
getsizeof: Callable[[_VT], float] | None = None,
86107
) -> None: ...
87108
@property
88-
def ttl(self) -> float: ...
89-
def expire(self, time: float | None = None) -> list[tuple[_KT, _VT]]: ...
109+
def ttl(self) -> Any: ...
110+
def expire(self, time: _TT | None = None) -> list[tuple[_KT, _VT]]: ...
90111

91-
class TLRUCache(_TimedCache[_KT, _VT]):
112+
class TLRUCache(_TimedCache[_KT, _VT, _TT]):
113+
@overload
114+
def __init__(
115+
self: TLRUCache[_KT2, _VT2, float],
116+
maxsize: float,
117+
ttu: Callable[[_KT2, _VT2, float], float],
118+
*,
119+
getsizeof: Callable[[_VT2], float] | None = None,
120+
) -> None: ...
121+
@overload
92122
def __init__(
93123
self,
94124
maxsize: float,
95-
ttu: Callable[[_KT, _VT, float], float],
96-
timer: Callable[[], float] = ...,
125+
ttu: Callable[[_KT, _VT, _TT], _TT],
126+
timer: Callable[[], _TT],
97127
getsizeof: Callable[[_VT], float] | None = None,
98128
) -> None: ...
99129
@property
100-
def ttu(self) -> Callable[[_KT, _VT, float], float]: ...
101-
def expire(self, time: float | None = None) -> list[tuple[_KT, _VT]]: ...
130+
def ttu(self) -> Callable[[_KT, _VT, _TT], _TT]: ...
131+
def expire(self, time: _TT | None = None) -> list[tuple[_KT, _VT]]: ...
102132

103133
class _CacheInfo(NamedTuple):
104134
hits: int
105135
misses: int
106-
maxsize: int | None
107-
currsize: int
136+
maxsize: float | None
137+
currsize: float
138+
139+
@type_check_only
140+
class _AbstractCondition(AbstractContextManager[Any], Protocol):
141+
# implementation an unit tests do not use plain wait() and notify()
142+
def wait_for(
143+
self, predicate: Callable[[], _T], timeout: float | None = None
144+
) -> _T: ...
145+
def notify_all(self) -> None: ...
108146

109147
@type_check_only
110148
class _cached_wrapper(Generic[_R]):
111149
__wrapped__: Callable[..., _R]
150+
__name__: str
151+
__doc__: str | None
152+
cache: MutableMapping[Any, Any] | None
153+
cache_key: Callable[..., Any] = ...
154+
cache_lock: AbstractContextManager[Any] | None = None
155+
cache_condition: _AbstractCondition | None = None
112156
def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ...
157+
def cache_clear(self) -> None: ...
113158

114159
@type_check_only
115160
class _cached_wrapper_info(_cached_wrapper[_R]):
116161
def cache_info(self) -> _CacheInfo: ...
117-
def cache_clear(self) -> None: ...
118162

119163
@overload
120164
def cached(
121165
cache: MutableMapping[_KT, Any] | None,
122166
key: Callable[..., _KT] = ...,
123167
lock: AbstractContextManager[Any] | None = None,
124-
condition: Condition | None = None,
168+
condition: _AbstractCondition | None = None,
125169
info: Literal[True] = ...,
126170
) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ...
127171
@overload
128172
def cached(
129173
cache: MutableMapping[_KT, Any] | None,
130174
key: Callable[..., _KT] = ...,
131175
lock: AbstractContextManager[Any] | None = None,
132-
condition: Condition | None = None,
133-
info: Literal[False] = ...,
176+
condition: _AbstractCondition | None = None,
177+
info: Literal[False] = False,
134178
) -> Callable[[Callable[..., _R]], _cached_wrapper[_R]]: ...
179+
@type_check_only
180+
class _cachedmethod_wrapper(Generic[_R]):
181+
__wrapped__: Callable[..., _R]
182+
__name__: str
183+
__doc__: str | None
184+
cache: MutableMapping[Any, Any] | None
185+
cache_key: Callable[..., Any] = ...
186+
cache_lock: AbstractContextManager[Any] | None = None
187+
cache_condition: _AbstractCondition | None = None
188+
def __call__(self, obj: Any, /, *args: Any, **kwargs: Any) -> _R: ...
189+
def cache_clear(self) -> None: ...
190+
191+
@type_check_only
192+
class _cachedmethod_wrapper_info(_cachedmethod_wrapper[_R]):
193+
def cache_info(self) -> _CacheInfo: ...
194+
135195
@overload
136-
@deprecated("Passing `info` as positional parameter is deprecated.")
137-
def cached(
138-
cache: MutableMapping[_KT, Any] | None,
196+
def cachedmethod(
197+
cache: Callable[[Any], MutableMapping[_KT, Any]],
139198
key: Callable[..., _KT] = ...,
140-
lock: AbstractContextManager[Any] | None = None,
141-
condition: Literal[True] = ...,
142-
) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ...
199+
lock: Callable[[Any], AbstractContextManager[Any]] | None = None,
200+
condition: Callable[[Any], _AbstractCondition] | None = None,
201+
info: Literal[True] = ...,
202+
) -> Callable[[Callable[..., _R]], _cachedmethod_wrapper_info[_R]]: ...
143203
@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]]: ...
151204
def cachedmethod(
152-
cache: Callable[[Any], MutableMapping[_KT, Any] | None],
205+
cache: Callable[[Any], MutableMapping[_KT, Any]],
153206
key: Callable[..., _KT] = ...,
154207
lock: Callable[[Any], AbstractContextManager[Any]] | None = None,
155-
condition: Condition | None = None,
156-
) -> IdentityFunction: ...
208+
condition: Callable[[Any], _AbstractCondition] | None = None,
209+
info: Literal[False] = False,
210+
) -> 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)