1- from _typeshed import IdentityFunction , Unused
1+ import random
22from collections .abc import Callable , Iterator , MutableMapping , Sequence
33from 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
1837class 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]): ...
4361class LRUCache (Cache [_KT , _VT ]): ...
4462
4563class 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
105133class _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
112150class _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
117162class _cached_wrapper_info (_cached_wrapper [_R ]):
118163 def cache_info (self ) -> _CacheInfo : ...
119- def cache_clear (self ) -> None : ...
120164
121165@overload
122166def 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
130174def 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
139199def 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
147207def 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 ]] : ...
0 commit comments