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" , default = float )
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+ ) -> None : ...
2141 def __getitem__ (self , key : _KT ) -> _VT : ...
2242 def __setitem__ (self , key : _KT , value : _VT ) -> None : ...
2343 def __delitem__ (self , key : _KT ) -> None : ...
@@ -41,116 +61,150 @@ class LFUCache(Cache[_KT, _VT]): ...
4161class LRUCache (Cache [_KT , _VT ]): ...
4262
4363class 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 : ...
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 : ...
5470 @property
5571 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 : ...
5872
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 : ...
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 : ...
6880
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 : ...
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 : ...
7487
7588 @property
76- def timer (self ) -> _Timer : ...
89+ def timer (self ) -> _Timer [ _TT ] : ...
7790
78- class TTLCache (_TimedCache [_KT , _VT ]):
79- @overload
80- def __init__ (self , maxsize : float , ttl : float , timer : Callable [[], float ] = ..., getsizeof : None = None ) -> None : ...
91+ class TTLCache (_TimedCache [_KT , _VT , _TT ]):
8192 @overload
82- 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 : ...
83100 @overload
84101 def __init__ (
85- 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 ,
86107 ) -> None : ...
87108 @property
88- def ttl (self ) -> float : ...
89- 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 ]]: ...
90111
91- 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
92122 def __init__ (
93123 self ,
94124 maxsize : float ,
95- ttu : Callable [[_KT , _VT , float ], float ],
96- timer : Callable [[], float ] = ... ,
125+ ttu : Callable [[_KT , _VT , _TT ], _TT ],
126+ timer : Callable [[], _TT ] ,
97127 getsizeof : Callable [[_VT ], float ] | None = None ,
98128 ) -> None : ...
99129 @property
100- def ttu (self ) -> Callable [[_KT , _VT , float ], float ]: ...
101- 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 ]]: ...
102132
103133class _CacheInfo (NamedTuple ):
104134 hits : int
105135 misses : int
106- maxsize : int | None
107- currsize : int
136+ maxsize : float | None
137+ currsize : float
138+
139+ @type_check_only
140+ class _AbstractCondition (AbstractContextManager [Any ], Protocol ):
141+ # implementation an unit tests do not use plain wait() and notify()
142+ def wait_for (
143+ self , predicate : Callable [[], _T ], timeout : float | None = None
144+ ) -> _T : ...
145+ def notify_all (self ) -> None : ...
108146
109147@type_check_only
110148class _cached_wrapper (Generic [_R ]):
111149 __wrapped__ : Callable [..., _R ]
150+ __name__ : str
151+ __doc__ : str | None
152+ cache : MutableMapping [Any , Any ] | None
153+ cache_key : Callable [..., Any ] = ...
154+ cache_lock : AbstractContextManager [Any ] | None = None
155+ cache_condition : _AbstractCondition | None = None
112156 def __call__ (self , / , * args : Any , ** kwargs : Any ) -> _R : ...
157+ def cache_clear (self ) -> None : ...
113158
114159@type_check_only
115160class _cached_wrapper_info (_cached_wrapper [_R ]):
116161 def cache_info (self ) -> _CacheInfo : ...
117- def cache_clear (self ) -> None : ...
118162
119163@overload
120164def cached (
121165 cache : MutableMapping [_KT , Any ] | None ,
122166 key : Callable [..., _KT ] = ...,
123167 lock : AbstractContextManager [Any ] | None = None ,
124- condition : Condition | None = None ,
168+ condition : _AbstractCondition | None = None ,
125169 info : Literal [True ] = ...,
126170) -> Callable [[Callable [..., _R ]], _cached_wrapper_info [_R ]]: ...
127171@overload
128172def cached (
129173 cache : MutableMapping [_KT , Any ] | None ,
130174 key : Callable [..., _KT ] = ...,
131175 lock : AbstractContextManager [Any ] | None = None ,
132- condition : Condition | None = None ,
133- info : Literal [False ] = ... ,
176+ condition : _AbstractCondition | None = None ,
177+ info : Literal [False ] = False ,
134178) -> Callable [[Callable [..., _R ]], _cached_wrapper [_R ]]: ...
179+ @type_check_only
180+ class _cachedmethod_wrapper (Generic [_R ]):
181+ __wrapped__ : Callable [..., _R ]
182+ __name__ : str
183+ __doc__ : str | None
184+ cache : MutableMapping [Any , Any ] | None
185+ cache_key : Callable [..., Any ] = ...
186+ cache_lock : AbstractContextManager [Any ] | None = None
187+ cache_condition : _AbstractCondition | None = None
188+ def __call__ (self , obj : Any , / , * args : Any , ** kwargs : Any ) -> _R : ...
189+ def cache_clear (self ) -> None : ...
190+
191+ @type_check_only
192+ class _cachedmethod_wrapper_info (_cachedmethod_wrapper [_R ]):
193+ def cache_info (self ) -> _CacheInfo : ...
194+
135195@overload
136- @deprecated ("Passing `info` as positional parameter is deprecated." )
137- def cached (
138- cache : MutableMapping [_KT , Any ] | None ,
196+ def cachedmethod (
197+ cache : Callable [[Any ], MutableMapping [_KT , Any ]],
139198 key : Callable [..., _KT ] = ...,
140- lock : AbstractContextManager [Any ] | None = None ,
141- condition : Literal [True ] = ...,
142- ) -> Callable [[Callable [..., _R ]], _cached_wrapper_info [_R ]]: ...
199+ lock : Callable [[Any ], AbstractContextManager [Any ]] | None = None ,
200+ condition : Callable [[Any ], _AbstractCondition ] | None = None ,
201+ info : Literal [True ] = ...,
202+ ) -> Callable [[Callable [..., _R ]], _cachedmethod_wrapper_info [_R ]]: ...
143203@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 ]]: ...
151204def cachedmethod (
152- cache : Callable [[Any ], MutableMapping [_KT , Any ] | None ],
205+ cache : Callable [[Any ], MutableMapping [_KT , Any ]],
153206 key : Callable [..., _KT ] = ...,
154207 lock : Callable [[Any ], AbstractContextManager [Any ]] | None = None ,
155- condition : Condition | None = None ,
156- ) -> IdentityFunction : ...
208+ condition : Callable [[Any ], _AbstractCondition ] | None = None ,
209+ info : Literal [False ] = False ,
210+ ) -> Callable [[Callable [..., _R ]], _cachedmethod_wrapper [_R ]]: ...
0 commit comments