Skip to content

Commit 09dd6fe

Browse files
committed
Improve original type stubs from typeshed.
1 parent 873c701 commit 09dd6fe

3 files changed

Lines changed: 159 additions & 72 deletions

File tree

src/cachetools/__init__.pyi

Lines changed: 120 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +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
7-
8-
from . import keys
9-
10-
__all__ = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod")
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+
)
15+
16+
__all__: Final = (
17+
"Cache",
18+
"FIFOCache",
19+
"LFUCache",
20+
"LRUCache",
21+
"RRCache",
22+
"TLRUCache",
23+
"TTLCache",
24+
"cached",
25+
"cachedmethod",
26+
)
1127
__version__: str
1228

1329
_KT = TypeVar("_KT")
1430
_VT = TypeVar("_VT")
31+
_TT = TypeVar("_TT", default=float)
1532
_T = TypeVar("_T")
1633
_R = TypeVar("_R")
34+
_KT2 = TypeVar("_KT2")
35+
_VT2 = TypeVar("_VT2")
1736

1837
class Cache(MutableMapping[_KT, _VT]):
19-
@overload
20-
def __init__(self, maxsize: float, getsizeof: Callable[[_VT], float]) -> None: ...
21-
@overload
22-
def __init__(self, maxsize: float, getsizeof: None = None) -> None: ...
38+
def __init__(
39+
self, maxsize: float, getsizeof: Callable[[_VT], float] | None = None
40+
) -> None: ...
2341
def __getitem__(self, key: _KT) -> _VT: ...
2442
def __setitem__(self, key: _KT, value: _VT) -> None: ...
2543
def __delitem__(self, key: _KT) -> None: ...
@@ -43,111 +61,153 @@ class LFUCache(Cache[_KT, _VT]): ...
4361
class LRUCache(Cache[_KT, _VT]): ...
4462

4563
class RRCache(Cache[_KT, _VT]):
46-
@overload
47-
def __init__(self, maxsize: float, choice: None = None, getsizeof: None = None) -> None: ...
48-
@overload
49-
def __init__(self, maxsize: float, *, getsizeof: Callable[[_VT], float]) -> None: ...
50-
@overload
51-
def __init__(self, maxsize: float, choice: None, getsizeof: Callable[[_VT], float]) -> None: ...
52-
@overload
53-
def __init__(self, maxsize: float, choice: Callable[[Sequence[_KT]], _KT], getsizeof: None = None) -> None: ...
54-
@overload
55-
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: ...
5670
@property
5771
def choice(self) -> Callable[[Sequence[_KT]], _KT]: ...
58-
def __setitem__(self, key: _KT, value: _VT, cache_setitem: Callable[[Self, _KT, _VT], None] = ...) -> None: ...
59-
def __delitem__(self, key: _KT, cache_delitem: Callable[[Self, _KT], None] = ...) -> None: ...
6072

61-
class _TimedCache(Cache[_KT, _VT]):
62-
@overload
63-
def __init__(self, maxsize: float, timer: Callable[[], float] = ..., getsizeof: None = None) -> None: ...
64-
@overload
65-
def __init__(self, maxsize: float, timer: Callable[[], float], getsizeof: Callable[[_VT], float]) -> None: ...
66-
@overload
67-
def __init__(self, maxsize: float, timer: Callable[[], float] = ..., *, getsizeof: Callable[[_VT], float]) -> None: ...
68-
@property
69-
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: ...
7080

71-
class _Timer:
72-
def __init__(self, timer: Callable[[], float]) -> None: ...
73-
def __call__(self) -> float: ...
74-
def __enter__(self) -> float: ...
75-
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: ...
7687

7788
@property
78-
def timer(self) -> _Timer: ...
89+
def timer(self) -> _Timer[_TT]: ...
7990

80-
class TTLCache(_TimedCache[_KT, _VT]):
81-
@overload
82-
def __init__(self, maxsize: float, ttl: float, timer: Callable[[], float] = ..., getsizeof: None = None) -> None: ...
91+
class TTLCache(_TimedCache[_KT, _VT, _TT]):
8392
@overload
84-
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: ...
85100
@overload
86101
def __init__(
87-
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,
88107
) -> None: ...
89108
@property
90-
def ttl(self) -> float: ...
91-
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]]: ...
92111

93-
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
94122
def __init__(
95123
self,
96124
maxsize: float,
97-
ttu: Callable[[_KT, _VT, float], float],
98-
timer: Callable[[], float] = ...,
125+
ttu: Callable[[_KT, _VT, _TT], _TT],
126+
timer: Callable[[], _TT],
99127
getsizeof: Callable[[_VT], float] | None = None,
100128
) -> None: ...
101129
@property
102-
def ttu(self) -> Callable[[_KT, _VT, float], float]: ...
103-
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]]: ...
104132

105133
class _CacheInfo(NamedTuple):
106134
hits: int
107135
misses: int
108-
maxsize: int | None
109-
currsize: int
136+
maxsize: float | None
137+
currsize: float
138+
139+
@type_check_only
140+
class _AbstractCondition(AbstractContextManager[Any], Protocol):
141+
# implementation and unit tests do not use plain wait() and notify()
142+
def wait(self, timeout: float | None = None) -> bool: ...
143+
def wait_for(
144+
self, predicate: Callable[[], _T], timeout: float | None = None
145+
) -> _T: ...
146+
def notify(self, n: int = 1) -> None: ...
147+
def notify_all(self) -> None: ...
110148

111149
@type_check_only
112150
class _cached_wrapper(Generic[_R]):
113151
__wrapped__: Callable[..., _R]
152+
__name__: str
153+
__doc__: str | None
154+
cache: MutableMapping[Any, Any] | None
155+
cache_key: Callable[..., Any] = ...
156+
cache_lock: AbstractContextManager[Any] | None = None
157+
cache_condition: _AbstractCondition | None = None
114158
def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ...
159+
def cache_clear(self) -> None: ...
115160

116161
@type_check_only
117162
class _cached_wrapper_info(_cached_wrapper[_R]):
118163
def cache_info(self) -> _CacheInfo: ...
119-
def cache_clear(self) -> None: ...
120164

121165
@overload
122166
def cached(
123167
cache: MutableMapping[_KT, Any] | None,
124168
key: Callable[..., _KT] = ...,
125169
lock: AbstractContextManager[Any] | None = None,
126-
condition: Condition | None = None,
170+
condition: _AbstractCondition | None = None,
127171
info: Literal[True] = ...,
128172
) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ...
129173
@overload
130174
def cached(
131175
cache: MutableMapping[_KT, Any] | None,
132176
key: Callable[..., _KT] = ...,
133177
lock: AbstractContextManager[Any] | None = None,
134-
condition: Condition | None = None,
178+
condition: _AbstractCondition | None = None,
135179
info: Literal[False] = ...,
136180
) -> Callable[[Callable[..., _R]], _cached_wrapper[_R]]: ...
137181

182+
@type_check_only
183+
class _cachedmethod_wrapper(Generic[_R]):
184+
__wrapped__: Callable[..., _R]
185+
__name__: str
186+
__doc__: str | None
187+
cache: MutableMapping[Any, Any] | None
188+
cache_key: Callable[..., Any] = ...
189+
cache_lock: AbstractContextManager[Any] | None = None
190+
cache_condition: _AbstractCondition | None = None
191+
def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ...
192+
def cache_clear(self) -> None: ...
193+
194+
@type_check_only
195+
class _cachedmethod_wrapper_info(_cachedmethod_wrapper[_R]):
196+
def cache_info(self) -> _CacheInfo: ...
197+
138198
@overload
139199
def cachedmethod(
140200
cache: Callable[[Any], MutableMapping[_KT, Any]],
141201
key: Callable[..., _KT] = ...,
142202
lock: Callable[[Any], AbstractContextManager[Any]] | None = None,
143-
condition: Callable[[Any], Condition] | None = None,
203+
condition: Callable[[Any], _AbstractCondition] | None = None,
144204
info: Literal[True] = ...,
145-
) -> IdentityFunction: ...
205+
) -> Callable[[Callable[..., _R]], _cachedmethod_wrapper_info[_R]]: ...
146206
@overload
147207
def cachedmethod(
148208
cache: Callable[[Any], MutableMapping[_KT, Any]],
149209
key: Callable[..., _KT] = ...,
150210
lock: Callable[[Any], AbstractContextManager[Any]] | None = None,
151-
condition: Callable[[Any], Condition] | None = None,
211+
condition: Callable[[Any], _AbstractCondition] | None = None,
152212
info: Literal[False] = ...,
153-
) -> IdentityFunction: ...
213+
) -> Callable[[Callable[..., _R]], _cachedmethod_wrapper[_R]]: ...

src/cachetools/func.pyi

Lines changed: 31 additions & 9 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: ...
@@ -21,28 +23,48 @@ def fifo_cache(
2123
maxsize: int | None = 128, typed: bool = False
2224
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
2325
@overload
24-
def fifo_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ...
26+
def fifo_cache(
27+
maxsize: Callable[..., _R], typed: bool = False
28+
) -> _cachetools_cache_wrapper[_R]: ...
2529
@overload
26-
def lfu_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
30+
def lfu_cache(
31+
maxsize: int | None = 128, typed: bool = False
32+
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
2733
@overload
28-
def lfu_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ...
34+
def lfu_cache(
35+
maxsize: Callable[..., _R], typed: bool = False
36+
) -> _cachetools_cache_wrapper[_R]: ...
2937
@overload
30-
def lru_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
38+
def lru_cache(
39+
maxsize: int | None = 128, typed: bool = False
40+
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
3141
@overload
32-
def lru_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ...
42+
def lru_cache(
43+
maxsize: Callable[..., _R], typed: bool = False
44+
) -> _cachetools_cache_wrapper[_R]: ...
3345
@overload
3446
def rr_cache(
35-
maxsize: int | None = 128, choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False
47+
maxsize: int | None = 128,
48+
choice: Callable[[Sequence[_T]], _T] = ...,
49+
typed: bool = False,
3650
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
3751
@overload
3852
def rr_cache(
39-
maxsize: Callable[..., _R], choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False
53+
maxsize: Callable[..., _R],
54+
choice: Callable[[Sequence[_T]], _T] = ...,
55+
typed: bool = False,
4056
) -> _cachetools_cache_wrapper[_R]: ...
4157
@overload
4258
def ttl_cache(
43-
maxsize: int | None = 128, ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False
59+
maxsize: int | None = 128,
60+
ttl: Any = 600,
61+
timer: Callable[[], _T] = ...,
62+
typed: bool = False,
4463
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
4564
@overload
4665
def ttl_cache(
47-
maxsize: Callable[..., _R], ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False
66+
maxsize: Callable[..., _R],
67+
ttl: Any = 600,
68+
timer: Callable[[], _T] = ...,
69+
typed: bool = False,
4870
) -> _cachetools_cache_wrapper[_R]: ...

src/cachetools/keys.pyi

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)