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
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+ )
715
8- __all__ = ("Cache" , "FIFOCache" , "LFUCache" , "LRUCache" , "RRCache" , "TLRUCache" , "TTLCache" , "cached" , "cachedmethod" )
16+ __all__ : Final = (
17+ "Cache" ,
18+ "FIFOCache" ,
19+ "LFUCache" ,
20+ "LRUCache" ,
21+ "RRCache" ,
22+ "TLRUCache" ,
23+ "TTLCache" ,
24+ "cached" ,
25+ "cachedmethod" ,
26+ )
927__version__ : str
1028
1129_KT = TypeVar ("_KT" )
1230_VT = TypeVar ("_VT" )
31+ _TT = TypeVar ("_TT" )
1332_T = TypeVar ("_T" )
1433_R = TypeVar ("_R" )
34+ _KT2 = TypeVar ("_KT2" )
35+ _VT2 = TypeVar ("_VT2" )
1536
1637class 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 : ...
38+ def __init__ (
39+ self , maxsize : float , getsizeof : Callable [[_VT ], float ] | None = None
40+ ): ...
2141 def __getitem__ (self , key : _KT ) -> _VT : ...
2242 def __setitem__ (self , key : _KT , value : _VT ) -> None : ...
2343 def __delitem__ (self , key : _KT ) -> None : ...
2444 def __missing__ (self , key : _KT ) -> _VT : ...
2545 def __iter__ (self ) -> Iterator [_KT ]: ...
2646 def __len__ (self ) -> int : ...
27- @overload
28- def pop (self , key : _KT ) -> _VT : ...
29- @overload
30- def pop (self , key : _KT , default : _VT | _T ) -> _VT | _T : ...
3147 def setdefault (self , key : _KT , default : _VT | None = None ) -> _VT : ...
3248 @property
3349 def maxsize (self ) -> float : ...
@@ -41,116 +57,150 @@ class LFUCache(Cache[_KT, _VT]): ...
4157class LRUCache (Cache [_KT , _VT ]): ...
4258
4359class 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 : ...
60+ def __init__ (
61+ self ,
62+ maxsize : float ,
63+ choice : Callable [[Sequence [_KT ]], _KT ] = random .choice ,
64+ getsizeof : Callable [[_VT ], float ] | None = None ,
65+ ) -> None : ...
5466 @property
5567 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 : ...
5868
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 : ...
69+ class _TimedCache (Cache [_KT , _VT ], Generic [_KT , _VT , _TT ]):
70+ def __init__ (
71+ self ,
72+ maxsize : float ,
73+ timer : Callable [[], _TT ],
74+ getsizeof : Callable [[_VT ], float ] | None = None ,
75+ ): ...
6876
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 : ...
77+ class _Timer (AbstractContextManager [_T ]):
78+ def __init__ (self , timer : Callable [[], _T ]) -> None : ...
79+ def __call__ (self ) -> _T : ...
80+ def __enter__ (self ) -> _T : ...
81+ def __exit__ (self , * exc : object ) -> None : ...
82+ def __getattr__ (self , name : str ) -> Any : ...
7483
7584 @property
76- def timer (self ) -> _Timer : ...
85+ def timer (self ) -> _Timer [ _TT ] : ...
7786
78- class TTLCache (_TimedCache [_KT , _VT ]):
79- @overload
80- def __init__ (self , maxsize : float , ttl : float , timer : Callable [[], float ] = ..., getsizeof : None = None ) -> None : ...
87+ class TTLCache (_TimedCache [_KT , _VT , _TT ]):
8188 @overload
82- def __init__ (self , maxsize : float , ttl : float , timer : Callable [[], float ], getsizeof : Callable [[_VT ], float ]) -> None : ...
89+ def __init__ (
90+ self : TTLCache [_KT2 , _VT2 , float ],
91+ maxsize : float ,
92+ ttl : float ,
93+ * ,
94+ getsizeof : Callable [[_VT2 ], float ] | None = None ,
95+ ): ...
8396 @overload
8497 def __init__ (
85- self , maxsize : float , ttl : float , timer : Callable [[], float ] = ..., * , getsizeof : Callable [[_VT ], float ]
86- ) -> None : ...
98+ self ,
99+ maxsize : float ,
100+ ttl : Any , # FIXME: must be "addable" to _TT
101+ timer : Callable [[], _TT ],
102+ getsizeof : Callable [[_VT ], float ] | None = None ,
103+ ): ...
87104 @property
88- def ttl (self ) -> float : ...
89- def expire (self , time : float | None = None ) -> list [tuple [_KT , _VT ]]: ...
105+ def ttl (self ) -> Any : ...
106+ def expire (self , time : _TT | None = None ) -> list [tuple [_KT , _VT ]]: ...
90107
91- class TLRUCache (_TimedCache [_KT , _VT ]):
108+ class TLRUCache (_TimedCache [_KT , _VT , _TT ]):
109+ @overload
110+ def __init__ (
111+ self : TLRUCache [_KT2 , _VT2 , float ],
112+ maxsize : float ,
113+ ttu : Callable [[_KT2 , _VT2 , float ], float ],
114+ * ,
115+ getsizeof : Callable [[_VT2 ], float ] | None = None ,
116+ ): ...
117+ @overload
92118 def __init__ (
93119 self ,
94120 maxsize : float ,
95- ttu : Callable [[_KT , _VT , float ], float ],
96- timer : Callable [[], float ] = ... ,
121+ ttu : Callable [[_KT , _VT , _TT ], _TT ],
122+ timer : Callable [[], _TT ] ,
97123 getsizeof : Callable [[_VT ], float ] | None = None ,
98- ) -> None : ...
124+ ): ...
99125 @property
100- def ttu (self ) -> Callable [[_KT , _VT , float ], float ]: ...
101- def expire (self , time : float | None = None ) -> list [tuple [_KT , _VT ]]: ...
126+ def ttu (self ) -> Callable [[_KT , _VT , _TT ], _TT ]: ...
127+ def expire (self , time : _TT | None = None ) -> list [tuple [_KT , _VT ]]: ...
102128
103129class _CacheInfo (NamedTuple ):
104130 hits : int
105131 misses : int
106- maxsize : int | None
107- currsize : int
132+ maxsize : float | None
133+ currsize : float
134+
135+ @type_check_only
136+ class _AbstractCondition (AbstractContextManager [Any ], Protocol ):
137+ # implementation an unit tests do not use plain wait() and notify()
138+ def wait_for (
139+ self , predicate : Callable [[], _T ], timeout : float | None = None
140+ ) -> _T : ...
141+ def notify_all (self ) -> None : ...
108142
109143@type_check_only
110144class _cached_wrapper (Generic [_R ]):
111145 __wrapped__ : Callable [..., _R ]
146+ __name__ : str
147+ __doc__ : str | None
148+ cache : MutableMapping [Any , Any ] | None
149+ cache_key : Callable [..., Any ] = ...
150+ cache_lock : AbstractContextManager [Any ] | None = None
151+ cache_condition : _AbstractCondition | None = None
112152 def __call__ (self , / , * args : Any , ** kwargs : Any ) -> _R : ...
153+ def cache_clear (self ) -> None : ...
113154
114155@type_check_only
115156class _cached_wrapper_info (_cached_wrapper [_R ]):
116157 def cache_info (self ) -> _CacheInfo : ...
117- def cache_clear (self ) -> None : ...
118158
119159@overload
120160def cached (
121161 cache : MutableMapping [_KT , Any ] | None ,
122162 key : Callable [..., _KT ] = ...,
123163 lock : AbstractContextManager [Any ] | None = None ,
124- condition : Condition | None = None ,
164+ condition : _AbstractCondition | None = None ,
125165 info : Literal [True ] = ...,
126166) -> Callable [[Callable [..., _R ]], _cached_wrapper_info [_R ]]: ...
127167@overload
128168def cached (
129169 cache : MutableMapping [_KT , Any ] | None ,
130170 key : Callable [..., _KT ] = ...,
131171 lock : AbstractContextManager [Any ] | None = None ,
132- condition : Condition | None = None ,
133- info : Literal [False ] = ... ,
172+ condition : _AbstractCondition | None = None ,
173+ info : Literal [False ] = False ,
134174) -> Callable [[Callable [..., _R ]], _cached_wrapper [_R ]]: ...
175+ @type_check_only
176+ class _cachedmethod_wrapper (Generic [_R ]):
177+ __wrapped__ : Callable [..., _R ]
178+ __name__ : str
179+ __doc__ : str | None
180+ cache : MutableMapping [Any , Any ] | None
181+ cache_key : Callable [..., Any ] = ...
182+ cache_lock : AbstractContextManager [Any ] | None = None
183+ cache_condition : _AbstractCondition | None = None
184+ def __call__ (self , obj , / , * args : Any , ** kwargs : Any ) -> _R : ...
185+ def cache_clear (self ) -> None : ...
186+
187+ @type_check_only
188+ class _cachedmethod_wrapper_info (_cachedmethod_wrapper [_R ]):
189+ def cache_info (self ) -> _CacheInfo : ...
190+
135191@overload
136- @deprecated ("Passing `info` as positional parameter is deprecated." )
137- def cached (
138- cache : MutableMapping [_KT , Any ] | None ,
192+ def cachedmethod (
193+ cache : Callable [[Any ], MutableMapping [_KT , Any ]],
139194 key : Callable [..., _KT ] = ...,
140- lock : AbstractContextManager [Any ] | None = None ,
141- condition : Literal [True ] = ...,
142- ) -> Callable [[Callable [..., _R ]], _cached_wrapper_info [_R ]]: ...
195+ lock : Callable [[Any ], AbstractContextManager [Any ]] | None = None ,
196+ condition : Callable [[Any ], _AbstractCondition ] | None = None ,
197+ info : Literal [True ] = ...,
198+ ) -> Callable [[Callable [..., _R ]], _cachedmethod_wrapper_info [_R ]]: ...
143199@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 ]]: ...
151200def cachedmethod (
152- cache : Callable [[Any ], MutableMapping [_KT , Any ] | None ],
201+ cache : Callable [[Any ], MutableMapping [_KT , Any ]],
153202 key : Callable [..., _KT ] = ...,
154203 lock : Callable [[Any ], AbstractContextManager [Any ]] | None = None ,
155- condition : Condition | None = None ,
156- ) -> IdentityFunction : ...
204+ condition : Callable [[Any ], _AbstractCondition ] | None = None ,
205+ info : Literal [False ] = False ,
206+ ) -> Callable [[Callable [..., _R ]], _cachedmethod_wrapper [_R ]]: ...
0 commit comments