|
1 | | -from _typeshed import IdentityFunction, Unused |
2 | 1 | from collections.abc import Callable, Iterator, MutableMapping, Sequence |
3 | 2 | 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 |
| 3 | +from typing import Any, Final, Generic, Literal, NamedTuple, Protocol, TypeVar, overload, type_check_only |
7 | 4 |
|
8 | | -__all__ = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod") |
| 5 | +__all__: Final = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod") |
9 | 6 | __version__: str |
10 | 7 |
|
11 | 8 | _KT = TypeVar("_KT") |
12 | 9 | _VT = TypeVar("_VT") |
| 10 | +_TT = TypeVar("_TT") |
13 | 11 | _T = TypeVar("_T") |
14 | 12 | _R = TypeVar("_R") |
15 | 13 |
|
16 | 14 | 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: ... |
| 15 | + def __init__(self, maxsize: float, getsizeof: Callable[[_VT], float] | None = None): ... |
21 | 16 | def __getitem__(self, key: _KT) -> _VT: ... |
22 | 17 | def __setitem__(self, key: _KT, value: _VT) -> None: ... |
23 | 18 | def __delitem__(self, key: _KT) -> None: ... |
@@ -53,104 +48,132 @@ class RRCache(Cache[_KT, _VT]): |
53 | 48 | def __init__(self, maxsize: float, choice: Callable[[Sequence[_KT]], _KT], getsizeof: Callable[[_VT], float]) -> None: ... |
54 | 49 | @property |
55 | 50 | 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: ... |
58 | 51 |
|
59 | 52 | class _TimedCache(Cache[_KT, _VT]): |
60 | 53 | @overload |
61 | | - def __init__(self, maxsize: float, timer: Callable[[], float] = ..., getsizeof: None = None) -> None: ... |
| 54 | + def __init__(self, maxsize: float, timer: Callable[[], _TT] = ..., getsizeof: None = None): ... |
62 | 55 | @overload |
63 | | - def __init__(self, maxsize: float, timer: Callable[[], float], getsizeof: Callable[[_VT], float]) -> None: ... |
| 56 | + def __init__(self, maxsize: float, timer: Callable[[], _TT], getsizeof: Callable[[_VT], float]): ... |
64 | 57 | @overload |
65 | | - def __init__(self, maxsize: float, timer: Callable[[], float] = ..., *, getsizeof: Callable[[_VT], float]) -> None: ... |
66 | | - @property |
67 | | - def currsize(self) -> float: ... |
| 58 | + def __init__(self, maxsize: float, timer: Callable[[], _TT] = ..., *, getsizeof: Callable[[_VT], float]): ... |
68 | 59 |
|
69 | 60 | 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: ... |
| 61 | + def __init__(self, timer: Callable[[], _TT]) -> None: ... |
| 62 | + def __call__(self) -> Any: ... |
| 63 | + def __enter__(self) -> Any: ... |
| 64 | + def __exit__(self, *exc: object) -> None: ... |
| 65 | + def __getattr__(self, name: str) -> Any: ... |
74 | 66 |
|
75 | 67 | @property |
76 | 68 | def timer(self) -> _Timer: ... |
77 | 69 |
|
78 | 70 | class TTLCache(_TimedCache[_KT, _VT]): |
79 | 71 | @overload |
80 | | - def __init__(self, maxsize: float, ttl: float, timer: Callable[[], float] = ..., getsizeof: None = None) -> None: ... |
| 72 | + def __init__(self, maxsize: float, ttl: Any, timer: Callable[..., _TT] = ..., getsizeof: None = None): ... |
81 | 73 | @overload |
82 | | - def __init__(self, maxsize: float, ttl: float, timer: Callable[[], float], getsizeof: Callable[[_VT], float]) -> None: ... |
| 74 | + def __init__(self, maxsize: float, ttl: Any, timer: Callable[..., _TT], getsizeof: Callable[[_VT], float]): ... |
83 | 75 | @overload |
84 | | - def __init__( |
85 | | - self, maxsize: float, ttl: float, timer: Callable[[], float] = ..., *, getsizeof: Callable[[_VT], float] |
86 | | - ) -> None: ... |
| 76 | + def __init__(self, maxsize: float, ttl: Any, timer: Callable[..., _TT] = ..., *, getsizeof: Callable[[_VT], float]): ... |
87 | 77 | @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: Any | None = None) -> list[tuple[_KT, _VT]]: ... |
90 | 80 |
|
91 | 81 | class TLRUCache(_TimedCache[_KT, _VT]): |
| 82 | + @overload |
| 83 | + def __init__( |
| 84 | + self, maxsize: float, ttu: Callable[[_KT, _VT, _TT], _TT], timer: Callable[..., _TT] = ..., getsizeof: None = None |
| 85 | + ): ... |
| 86 | + @overload |
| 87 | + def __init__( |
| 88 | + self, maxsize: float, ttu: Callable[[_KT, _VT, _TT], _TT], timer: Callable[..., _TT], getsizeof: Callable[[_VT], float] |
| 89 | + ): ... |
| 90 | + @overload |
92 | 91 | def __init__( |
93 | 92 | self, |
94 | 93 | maxsize: float, |
95 | | - ttu: Callable[[_KT, _VT, float], float], |
96 | | - timer: Callable[[], float] = ..., |
97 | | - getsizeof: Callable[[_VT], float] | None = None, |
98 | | - ) -> None: ... |
| 94 | + ttu: Callable[[_KT, _VT, _TT], _TT], |
| 95 | + timer: Callable[..., _TT] = ..., |
| 96 | + *, |
| 97 | + getsizeof: Callable[[_VT], float], |
| 98 | + ): ... |
99 | 99 | @property |
100 | | - def ttu(self) -> Callable[[_KT, _VT, float], float]: ... |
101 | | - def expire(self, time: float | None = None) -> list[tuple[_KT, _VT]]: ... |
| 100 | + def ttu(self) -> Callable[[_KT, _VT, _TT], _TT]: ... |
| 101 | + def expire(self, time: Any | None = None) -> list[tuple[_KT, _VT]]: ... |
102 | 102 |
|
103 | 103 | class _CacheInfo(NamedTuple): |
104 | 104 | hits: int |
105 | 105 | misses: int |
106 | 106 | maxsize: int | None |
107 | 107 | currsize: int |
108 | 108 |
|
| 109 | +@type_check_only |
| 110 | +class _AbstractCondition(AbstractContextManager[Any], Protocol): |
| 111 | + # def wait(self, timeout: float | None = None) -> bool: ... |
| 112 | + def wait_for(self, predicate: Callable[[], _T], timeout: float | None = None) -> _T: ... |
| 113 | + # def notify(self, n: int = 1) -> None: ... |
| 114 | + def notify_all(self) -> None: ... |
| 115 | + |
109 | 116 | @type_check_only |
110 | 117 | class _cached_wrapper(Generic[_R]): |
111 | 118 | __wrapped__: Callable[..., _R] |
| 119 | + __name__: str |
| 120 | + __doc__: str | None |
| 121 | + cache: MutableMapping[Any, Any] | None |
| 122 | + cache_key: Callable[..., Any] = ... |
| 123 | + cache_lock: AbstractContextManager[Any] | None = None |
| 124 | + cache_condition: _AbstractCondition | None = None |
112 | 125 | def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ... |
| 126 | + def cache_clear(self) -> None: ... |
113 | 127 |
|
114 | 128 | @type_check_only |
115 | 129 | class _cached_wrapper_info(_cached_wrapper[_R]): |
116 | 130 | def cache_info(self) -> _CacheInfo: ... |
117 | | - def cache_clear(self) -> None: ... |
118 | 131 |
|
119 | 132 | @overload |
120 | 133 | def cached( |
121 | 134 | cache: MutableMapping[_KT, Any] | None, |
122 | 135 | key: Callable[..., _KT] = ..., |
123 | 136 | lock: AbstractContextManager[Any] | None = None, |
124 | | - condition: Condition | None = None, |
| 137 | + condition: _AbstractCondition | None = None, |
125 | 138 | info: Literal[True] = ..., |
126 | 139 | ) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ... |
127 | 140 | @overload |
128 | 141 | def cached( |
129 | 142 | cache: MutableMapping[_KT, Any] | None, |
130 | 143 | key: Callable[..., _KT] = ..., |
131 | 144 | lock: AbstractContextManager[Any] | None = None, |
132 | | - condition: Condition | None = None, |
| 145 | + condition: _AbstractCondition | None = None, |
133 | 146 | info: Literal[False] = ..., |
134 | 147 | ) -> Callable[[Callable[..., _R]], _cached_wrapper[_R]]: ... |
| 148 | +@type_check_only |
| 149 | +class _cachedmethod_wrapper(Generic[_R]): |
| 150 | + __wrapped__: Callable[..., _R] |
| 151 | + __name__: str |
| 152 | + __doc__: str | None |
| 153 | + cache: MutableMapping[Any, Any] | None |
| 154 | + cache_key: Callable[..., Any] = ... |
| 155 | + cache_lock: AbstractContextManager[Any] | None = None |
| 156 | + cache_condition: _AbstractCondition | None = None |
| 157 | + def __call__(self, obj, /, *args: Any, **kwargs: Any) -> _R: ... |
| 158 | + def cache_clear(self) -> None: ... |
| 159 | + |
| 160 | +@type_check_only |
| 161 | +class _cachedmethod_wrapper_info(_cachedmethod_wrapper[_R]): |
| 162 | + def cache_info(self) -> _CacheInfo: ... |
| 163 | + |
135 | 164 | @overload |
136 | | -@deprecated("Passing `info` as positional parameter is deprecated.") |
137 | | -def cached( |
138 | | - cache: MutableMapping[_KT, Any] | None, |
| 165 | +def cachedmethod( |
| 166 | + cache: Callable[[Any], MutableMapping[_KT, Any]], |
139 | 167 | key: Callable[..., _KT] = ..., |
140 | | - lock: AbstractContextManager[Any] | None = None, |
141 | | - condition: Literal[True] = ..., |
142 | | -) -> Callable[[Callable[..., _R]], _cached_wrapper_info[_R]]: ... |
| 168 | + lock: Callable[[Any], AbstractContextManager[Any]] | None = None, |
| 169 | + condition: Callable[[Any], _AbstractCondition] | None = None, |
| 170 | + info: Literal[True] = ..., |
| 171 | +) -> Callable[[Callable[..., _R]], _cachedmethod_wrapper_info[_R]]: ... |
143 | 172 | @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]]: ... |
151 | 173 | def cachedmethod( |
152 | | - cache: Callable[[Any], MutableMapping[_KT, Any] | None], |
| 174 | + cache: Callable[[Any], MutableMapping[_KT, Any]], |
153 | 175 | key: Callable[..., _KT] = ..., |
154 | 176 | lock: Callable[[Any], AbstractContextManager[Any]] | None = None, |
155 | | - condition: Condition | None = None, |
156 | | -) -> IdentityFunction: ... |
| 177 | + condition: Callable[[Any], _AbstractCondition] | None = None, |
| 178 | + info: Literal[False] = ..., |
| 179 | +) -> Callable[[Callable[..., _R]], _cachedmethod_wrapper[_R]]: ... |
0 commit comments