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 Any , Final , Generic , Literal , NamedTuple , Protocol , TypeVar , overload , type_check_only
75
8- __all__ = ("Cache" , "FIFOCache" , "LFUCache" , "LRUCache" , "RRCache" , "TLRUCache" , "TTLCache" , "cached" , "cachedmethod" )
6+ __all__ : Final = ("Cache" , "FIFOCache" , "LFUCache" , "LRUCache" , "RRCache" , "TLRUCache" , "TTLCache" , "cached" , "cachedmethod" )
97__version__ : str
108
119_KT = TypeVar ("_KT" )
1210_VT = TypeVar ("_VT" )
11+ _TT = TypeVar ("_TT" , default = float )
1312_T = TypeVar ("_T" )
1413_R = TypeVar ("_R" )
14+ _KT2 = TypeVar ("_KT2" )
15+ _VT2 = TypeVar ("_VT2" )
1516
1617class 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 ) -> None : ...
2119 def __getitem__ (self , key : _KT ) -> _VT : ...
2220 def __setitem__ (self , key : _KT , value : _VT ) -> None : ...
2321 def __delitem__ (self , key : _KT ) -> None : ...
@@ -41,116 +39,140 @@ class LFUCache(Cache[_KT, _VT]): ...
4139class LRUCache (Cache [_KT , _VT ]): ...
4240
4341class 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 : ...
42+ def __init__ (
43+ self ,
44+ maxsize : float ,
45+ choice : Callable [[Sequence [_KT ]], _KT ] = random .choice ,
46+ getsizeof : Callable [[_VT ], float ] | None = None ,
47+ ) -> None : ...
5448 @property
5549 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 : ...
5850
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 : ...
51+ class _TimedCache (Cache [_KT , _VT ], Generic [_KT , _VT , _TT ]):
52+ def __init__ (self , maxsize : float , timer : Callable [[], _TT ], getsizeof : Callable [[_VT ], float ] | None = None ) -> None : ...
6853
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 : ...
54+ class _Timer (AbstractContextManager [_T ]):
55+ def __init__ (self , timer : Callable [[], _T ]) -> None : ...
56+ def __call__ (self ) -> _T : ...
57+ def __enter__ (self ) -> _T : ...
58+ def __exit__ (self , * exc : object ) -> None : ...
59+ def __getattr__ (self , name : str ) -> Any : ...
7460
7561 @property
76- def timer (self ) -> _Timer : ...
62+ def timer (self ) -> _Timer [ _TT ] : ...
7763
78- class TTLCache (_TimedCache [_KT , _VT ]):
79- @overload
80- def __init__ (self , maxsize : float , ttl : float , timer : Callable [[], float ] = ..., getsizeof : None = None ) -> None : ...
64+ class TTLCache (_TimedCache [_KT , _VT , _TT ]):
8165 @overload
82- def __init__ (self , maxsize : float , ttl : float , timer : Callable [[], float ], getsizeof : Callable [[_VT ], float ]) -> None : ...
66+ def __init__ (
67+ self : TTLCache [_KT2 , _VT2 , float ], maxsize : float , ttl : float , * , getsizeof : Callable [[_VT2 ], float ] | None = None
68+ ) -> None : ...
8369 @overload
8470 def __init__ (
85- self , maxsize : float , ttl : float , timer : Callable [[], float ] = ..., * , getsizeof : Callable [[_VT ], float ]
71+ self ,
72+ maxsize : float ,
73+ ttl : Any , # FIXME: must be "addable" to _TT
74+ timer : Callable [[], _TT ],
75+ getsizeof : Callable [[_VT ], float ] | None = None ,
8676 ) -> None : ...
8777 @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 : _TT | None = None ) -> list [tuple [_KT , _VT ]]: ...
9080
91- class TLRUCache (_TimedCache [_KT , _VT ]):
81+ class TLRUCache (_TimedCache [_KT , _VT , _TT ]):
82+ @overload
83+ def __init__ (
84+ self : TLRUCache [_KT2 , _VT2 , float ],
85+ maxsize : float ,
86+ ttu : Callable [[_KT2 , _VT2 , float ], float ],
87+ * ,
88+ getsizeof : Callable [[_VT2 ], float ] | None = None ,
89+ ) -> None : ...
90+ @overload
9291 def __init__ (
9392 self ,
9493 maxsize : float ,
95- ttu : Callable [[_KT , _VT , float ], float ],
96- timer : Callable [[], float ] = ... ,
94+ ttu : Callable [[_KT , _VT , _TT ], _TT ],
95+ timer : Callable [[], _TT ] ,
9796 getsizeof : Callable [[_VT ], float ] | None = None ,
9897 ) -> None : ...
9998 @property
100- def ttu (self ) -> Callable [[_KT , _VT , float ], float ]: ...
101- def expire (self , time : float | None = None ) -> list [tuple [_KT , _VT ]]: ...
99+ def ttu (self ) -> Callable [[_KT , _VT , _TT ], _TT ]: ...
100+ def expire (self , time : _TT | None = None ) -> list [tuple [_KT , _VT ]]: ...
102101
103102class _CacheInfo (NamedTuple ):
104103 hits : int
105104 misses : int
106- maxsize : int | None
107- currsize : int
105+ maxsize : float | None
106+ currsize : float
107+
108+ @type_check_only
109+ class _AbstractCondition (AbstractContextManager [Any ], Protocol ):
110+ def wait (self , timeout : float | None = None ) -> bool : ...
111+ def wait_for (self , predicate : Callable [[], _T ], timeout : float | None = None ) -> _T : ...
112+ def notify (self , n : int = 1 ) -> None : ...
113+ def notify_all (self ) -> None : ...
108114
109115@type_check_only
110116class _cached_wrapper (Generic [_R ]):
111117 __wrapped__ : Callable [..., _R ]
118+ __name__ : str
119+ __doc__ : str | None
120+ cache : MutableMapping [Any , Any ] | None
121+ cache_key : Callable [..., Any ] = ...
122+ cache_lock : AbstractContextManager [Any ] | None = None
123+ cache_condition : _AbstractCondition | None = None
112124 def __call__ (self , / , * args : Any , ** kwargs : Any ) -> _R : ...
125+ def cache_clear (self ) -> None : ...
113126
114127@type_check_only
115128class _cached_wrapper_info (_cached_wrapper [_R ]):
116129 def cache_info (self ) -> _CacheInfo : ...
117- def cache_clear (self ) -> None : ...
118130
119131@overload
120132def cached (
121133 cache : MutableMapping [_KT , Any ] | None ,
122134 key : Callable [..., _KT ] = ...,
123135 lock : AbstractContextManager [Any ] | None = None ,
124- condition : Condition | None = None ,
136+ condition : _AbstractCondition | None = None ,
125137 info : Literal [True ] = ...,
126138) -> Callable [[Callable [..., _R ]], _cached_wrapper_info [_R ]]: ...
127139@overload
128140def cached (
129141 cache : MutableMapping [_KT , Any ] | None ,
130142 key : Callable [..., _KT ] = ...,
131143 lock : AbstractContextManager [Any ] | None = None ,
132- condition : Condition | None = None ,
144+ condition : _AbstractCondition | None = None ,
133145 info : Literal [False ] = ...,
134146) -> Callable [[Callable [..., _R ]], _cached_wrapper [_R ]]: ...
147+ @type_check_only
148+ class _cachedmethod_wrapper (Generic [_R ]):
149+ __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
156+ def __call__ (self , / , * args : Any , ** kwargs : Any ) -> _R : ...
157+ def cache_clear (self ) -> None : ...
158+
159+ @type_check_only
160+ class _cachedmethod_wrapper_info (_cachedmethod_wrapper [_R ]):
161+ def cache_info (self ) -> _CacheInfo : ...
162+
135163@overload
136- @deprecated ("Passing `info` as positional parameter is deprecated." )
137- def cached (
138- cache : MutableMapping [_KT , Any ] | None ,
164+ def cachedmethod (
165+ cache : Callable [[Any ], MutableMapping [_KT , Any ]],
139166 key : Callable [..., _KT ] = ...,
140- lock : AbstractContextManager [Any ] | None = None ,
141- condition : Literal [True ] = ...,
142- ) -> Callable [[Callable [..., _R ]], _cached_wrapper_info [_R ]]: ...
167+ lock : Callable [[Any ], AbstractContextManager [Any ]] | None = None ,
168+ condition : Callable [[Any ], _AbstractCondition ] | None = None ,
169+ info : Literal [True ] = ...,
170+ ) -> Callable [[Callable [..., _R ]], _cachedmethod_wrapper_info [_R ]]: ...
143171@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 ]]: ...
151172def cachedmethod (
152- cache : Callable [[Any ], MutableMapping [_KT , Any ] | None ],
173+ cache : Callable [[Any ], MutableMapping [_KT , Any ]],
153174 key : Callable [..., _KT ] = ...,
154175 lock : Callable [[Any ], AbstractContextManager [Any ]] | None = None ,
155- condition : Condition | None = None ,
156- ) -> IdentityFunction : ...
176+ condition : Callable [[Any ], _AbstractCondition ] | None = None ,
177+ info : Literal [False ] = ...,
178+ ) -> Callable [[Callable [..., _R ]], _cachedmethod_wrapper [_R ]]: ...
0 commit comments