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