Skip to content

Commit 62752e0

Browse files
authored
[cachetools] Update to 7.0.* (#15357) (#15657)
1 parent 1b250a2 commit 62752e0

6 files changed

Lines changed: 103 additions & 76 deletions

File tree

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: 91 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
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 Any, Final, Generic, Literal, NamedTuple, Protocol, TypeVar, overload, type_check_only
75

8-
__all__ = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod")
6+
__all__: Final = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod")
97
__version__: str
108

119
_KT = TypeVar("_KT")
1210
_VT = TypeVar("_VT")
11+
_TT = TypeVar("_TT", default=float)
1312
_T = TypeVar("_T")
1413
_R = TypeVar("_R")
14+
_KT2 = TypeVar("_KT2")
15+
_VT2 = TypeVar("_VT2")
1516

1617
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: ...
18+
def __init__(self, maxsize: float, getsizeof: Callable[[_VT], float] | None = None) -> None: ...
2119
def __getitem__(self, key: _KT) -> _VT: ...
2220
def __setitem__(self, key: _KT, value: _VT) -> None: ...
2321
def __delitem__(self, key: _KT) -> None: ...
@@ -41,116 +39,140 @@ class LFUCache(Cache[_KT, _VT]): ...
4139
class LRUCache(Cache[_KT, _VT]): ...
4240

4341
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: ...
42+
def __init__(
43+
self,
44+
maxsize: float,
45+
choice: Callable[[Sequence[_KT]], _KT] = random.choice,
46+
getsizeof: Callable[[_VT], float] | None = None,
47+
) -> None: ...
5448
@property
5549
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: ...
5850

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: ...
51+
class _TimedCache(Cache[_KT, _VT], Generic[_KT, _VT, _TT]):
52+
def __init__(self, maxsize: float, timer: Callable[[], _TT], getsizeof: Callable[[_VT], float] | None = None) -> None: ...
6853

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: ...
54+
class _Timer(AbstractContextManager[_T]):
55+
def __init__(self, timer: Callable[[], _T]) -> None: ...
56+
def __call__(self) -> _T: ...
57+
def __enter__(self) -> _T: ...
58+
def __exit__(self, *exc: object) -> None: ...
59+
def __getattr__(self, name: str) -> Any: ...
7460

7561
@property
76-
def timer(self) -> _Timer: ...
62+
def timer(self) -> _Timer[_TT]: ...
7763

78-
class TTLCache(_TimedCache[_KT, _VT]):
79-
@overload
80-
def __init__(self, maxsize: float, ttl: float, timer: Callable[[], float] = ..., getsizeof: None = None) -> None: ...
64+
class TTLCache(_TimedCache[_KT, _VT, _TT]):
8165
@overload
82-
def __init__(self, maxsize: float, ttl: float, timer: Callable[[], float], getsizeof: Callable[[_VT], float]) -> None: ...
66+
def __init__(
67+
self: TTLCache[_KT2, _VT2, float], maxsize: float, ttl: float, *, getsizeof: Callable[[_VT2], float] | None = None
68+
) -> None: ...
8369
@overload
8470
def __init__(
85-
self, maxsize: float, ttl: float, timer: Callable[[], float] = ..., *, getsizeof: Callable[[_VT], float]
71+
self,
72+
maxsize: float,
73+
ttl: Any, # FIXME: must be "addable" to _TT
74+
timer: Callable[[], _TT],
75+
getsizeof: Callable[[_VT], float] | None = None,
8676
) -> None: ...
8777
@property
88-
def ttl(self) -> float: ...
89-
def expire(self, time: float | None = None) -> list[tuple[_KT, _VT]]: ...
78+
def ttl(self) -> Any: ...
79+
def expire(self, time: _TT | None = None) -> list[tuple[_KT, _VT]]: ...
9080

91-
class TLRUCache(_TimedCache[_KT, _VT]):
81+
class TLRUCache(_TimedCache[_KT, _VT, _TT]):
82+
@overload
83+
def __init__(
84+
self: TLRUCache[_KT2, _VT2, float],
85+
maxsize: float,
86+
ttu: Callable[[_KT2, _VT2, float], float],
87+
*,
88+
getsizeof: Callable[[_VT2], float] | None = None,
89+
) -> None: ...
90+
@overload
9291
def __init__(
9392
self,
9493
maxsize: float,
95-
ttu: Callable[[_KT, _VT, float], float],
96-
timer: Callable[[], float] = ...,
94+
ttu: Callable[[_KT, _VT, _TT], _TT],
95+
timer: Callable[[], _TT],
9796
getsizeof: Callable[[_VT], float] | None = None,
9897
) -> None: ...
9998
@property
100-
def ttu(self) -> Callable[[_KT, _VT, float], float]: ...
101-
def expire(self, time: float | None = None) -> list[tuple[_KT, _VT]]: ...
99+
def ttu(self) -> Callable[[_KT, _VT, _TT], _TT]: ...
100+
def expire(self, time: _TT | None = None) -> list[tuple[_KT, _VT]]: ...
102101

103102
class _CacheInfo(NamedTuple):
104103
hits: int
105104
misses: int
106-
maxsize: int | None
107-
currsize: int
105+
maxsize: float | None
106+
currsize: float
107+
108+
@type_check_only
109+
class _AbstractCondition(AbstractContextManager[Any], Protocol):
110+
def wait(self, timeout: float | None = None) -> bool: ...
111+
def wait_for(self, predicate: Callable[[], _T], timeout: float | None = None) -> _T: ...
112+
def notify(self, n: int = 1) -> None: ...
113+
def notify_all(self) -> None: ...
108114

109115
@type_check_only
110116
class _cached_wrapper(Generic[_R]):
111117
__wrapped__: Callable[..., _R]
118+
__name__: str
119+
__doc__: str | None
120+
cache: MutableMapping[Any, Any] | None
121+
cache_key: Callable[..., Any] = ...
122+
cache_lock: AbstractContextManager[Any] | None = None
123+
cache_condition: _AbstractCondition | None = None
112124
def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ...
125+
def cache_clear(self) -> None: ...
113126

114127
@type_check_only
115128
class _cached_wrapper_info(_cached_wrapper[_R]):
116129
def cache_info(self) -> _CacheInfo: ...
117-
def cache_clear(self) -> None: ...
118130

119131
@overload
120132
def cached(
121133
cache: MutableMapping[_KT, Any] | None,
122134
key: Callable[..., _KT] = ...,
123135
lock: AbstractContextManager[Any] | None = None,
124-
condition: Condition | None = None,
136+
condition: _AbstractCondition | None = None,
125137
info: Literal[True] = ...,
126138
) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ...
127139
@overload
128140
def cached(
129141
cache: MutableMapping[_KT, Any] | None,
130142
key: Callable[..., _KT] = ...,
131143
lock: AbstractContextManager[Any] | None = None,
132-
condition: Condition | None = None,
144+
condition: _AbstractCondition | None = None,
133145
info: Literal[False] = ...,
134146
) -> Callable[[Callable[..., _R]], _cached_wrapper[_R]]: ...
147+
@type_check_only
148+
class _cachedmethod_wrapper(Generic[_R]):
149+
__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
156+
def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ...
157+
def cache_clear(self) -> None: ...
158+
159+
@type_check_only
160+
class _cachedmethod_wrapper_info(_cachedmethod_wrapper[_R]):
161+
def cache_info(self) -> _CacheInfo: ...
162+
135163
@overload
136-
@deprecated("Passing `info` as positional parameter is deprecated.")
137-
def cached(
138-
cache: MutableMapping[_KT, Any] | None,
164+
def cachedmethod(
165+
cache: Callable[[Any], MutableMapping[_KT, Any]],
139166
key: Callable[..., _KT] = ...,
140-
lock: AbstractContextManager[Any] | None = None,
141-
condition: Literal[True] = ...,
142-
) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ...
167+
lock: Callable[[Any], AbstractContextManager[Any]] | None = None,
168+
condition: Callable[[Any], _AbstractCondition] | None = None,
169+
info: Literal[True] = ...,
170+
) -> Callable[[Callable[..., _R]], _cachedmethod_wrapper_info[_R]]: ...
143171
@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]]: ...
151172
def cachedmethod(
152-
cache: Callable[[Any], MutableMapping[_KT, Any] | None],
173+
cache: Callable[[Any], MutableMapping[_KT, Any]],
153174
key: Callable[..., _KT] = ...,
154175
lock: Callable[[Any], AbstractContextManager[Any]] | None = None,
155-
condition: Condition | None = None,
156-
) -> IdentityFunction: ...
176+
condition: Callable[[Any], _AbstractCondition] | None = None,
177+
info: Literal[False] = ...,
178+
) -> Callable[[Callable[..., _R]], _cachedmethod_wrapper[_R]]: ...

stubs/cachetools/cachetools/func.pyi

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ _R = TypeVar("_R")
1111
@type_check_only
1212
class _cachetools_cache_wrapper(Generic[_R]):
1313
__wrapped__: Callable[..., _R]
14+
__name__: str
15+
__doc__: str | None
1416
def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ...
1517
def cache_info(self) -> _CacheInfo: ...
1618
def cache_clear(self) -> None: ...
@@ -32,17 +34,17 @@ def lru_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Calla
3234
def lru_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ...
3335
@overload
3436
def rr_cache(
35-
maxsize: int | None = 128, choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False
37+
maxsize: int | None = 128, choice: Callable[[Sequence[_T]], _T] = ..., typed: bool = False
3638
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
3739
@overload
3840
def rr_cache(
39-
maxsize: Callable[..., _R], choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False
41+
maxsize: Callable[..., _R], choice: Callable[[Sequence[_T]], _T] = ..., typed: bool = False
4042
) -> _cachetools_cache_wrapper[_R]: ...
4143
@overload
4244
def ttl_cache(
43-
maxsize: int | None = 128, ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False
45+
maxsize: int | None = 128, ttl: Any = 600, timer: Callable[[], _T] = ..., typed: bool = False
4446
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
4547
@overload
4648
def ttl_cache(
47-
maxsize: Callable[..., _R], ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False
49+
maxsize: Callable[..., _R], ttl: Any = 600, timer: Callable[[], _T] = ..., typed: bool = False
4850
) -> _cachetools_cache_wrapper[_R]: ...

stubs/cachetools/cachetools/keys.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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, ...]: ...
78
def methodkey(self: Unused, /, *args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ...

0 commit comments

Comments
 (0)