11from collections .abc import Callable , Iterator , MutableMapping , Sequence
22from contextlib import AbstractContextManager
3+ import random
4+ import time
35from typing import Any , Final , Generic , Literal , NamedTuple , Protocol , TypeVar , overload , type_check_only
46
57__all__ : Final = ("Cache" , "FIFOCache" , "LFUCache" , "LRUCache" , "RRCache" , "TLRUCache" , "TTLCache" , "cached" , "cachedmethod" )
@@ -36,26 +38,12 @@ class LFUCache(Cache[_KT, _VT]): ...
3638class LRUCache (Cache [_KT , _VT ]): ...
3739
3840class RRCache (Cache [_KT , _VT ]):
39- @overload
40- def __init__ (self , maxsize : float , choice : None = None , getsizeof : None = None ) -> None : ...
41- @overload
42- def __init__ (self , maxsize : float , * , getsizeof : Callable [[_VT ], float ]) -> None : ...
43- @overload
44- def __init__ (self , maxsize : float , choice : None , getsizeof : Callable [[_VT ], float ]) -> None : ...
45- @overload
46- def __init__ (self , maxsize : float , choice : Callable [[Sequence [_KT ]], _KT ], getsizeof : None = None ) -> None : ...
47- @overload
48- def __init__ (self , maxsize : float , choice : Callable [[Sequence [_KT ]], _KT ], getsizeof : Callable [[_VT ], float ]) -> None : ...
41+ def __init__ (self , maxsize : float , choice : Callable [[Sequence [_KT ]], _KT ] = random .choice , getsizeof : Callable [[_VT ], float ] | None = None ) -> None : ...
4942 @property
5043 def choice (self ) -> Callable [[Sequence [_KT ]], _KT ]: ...
5144
52- class _TimedCache (Cache [_KT , _VT ]):
53- @overload
54- def __init__ (self , maxsize : float , timer : Callable [[], _TT ] = ..., getsizeof : None = None ): ...
55- @overload
56- def __init__ (self , maxsize : float , timer : Callable [[], _TT ], getsizeof : Callable [[_VT ], float ]): ...
57- @overload
58- def __init__ (self , maxsize : float , timer : Callable [[], _TT ] = ..., * , getsizeof : Callable [[_VT ], float ]): ...
45+ class _TimedCache [_KT , _VT , _TT ](Cache [_KT , _VT ]):
46+ def __init__ (self , maxsize : float , timer : Callable [..., _TT ] = time .monotonic , getsizeof : Callable [[_VT ], float ] | None = None ): ...
5947
6048 class _Timer :
6149 def __init__ (self , timer : Callable [[], _TT ]) -> None : ...
@@ -67,38 +55,18 @@ class _TimedCache(Cache[_KT, _VT]):
6755 @property
6856 def timer (self ) -> _Timer : ...
6957
70- class TTLCache (_TimedCache [_KT , _VT ]):
71- @overload
72- def __init__ (self , maxsize : float , ttl : Any , timer : Callable [..., _TT ] = ..., getsizeof : None = None ): ...
73- @overload
74- def __init__ (self , maxsize : float , ttl : Any , timer : Callable [..., _TT ], getsizeof : Callable [[_VT ], float ]): ...
75- @overload
76- def __init__ (self , maxsize : float , ttl : Any , timer : Callable [..., _TT ] = ..., * , getsizeof : Callable [[_VT ], float ]): ...
58+ # FIXME: ttl should be "addable" to _TT
59+ class TTLCache (_TimedCache [_KT , _VT , _TT ]):
60+ def __init__ (self , maxsize : float , ttl : Any , timer : Callable [..., _TT ] = time .monotonic , getsizeof : Callable [[_VT ], float ] | None = None ): ...
7761 @property
7862 def ttl (self ) -> Any : ...
79- def expire (self , time : Any | None = None ) -> list [tuple [_KT , _VT ]]: ...
63+ def expire (self , time : _TT | None = None ) -> list [tuple [_KT , _VT ]]: ...
8064
81- class TLRUCache (_TimedCache [_KT , _VT ]):
82- @overload
83- def __init__ (
84- self , maxsize : float , ttu : Callable [[_KT , _VT , _TT ], _TT ], timer : Callable [..., _TT ] = ..., getsizeof : None = None
85- ): ...
86- @overload
87- def __init__ (
88- self , maxsize : float , ttu : Callable [[_KT , _VT , _TT ], _TT ], timer : Callable [..., _TT ], getsizeof : Callable [[_VT ], float ]
89- ): ...
90- @overload
91- def __init__ (
92- self ,
93- maxsize : float ,
94- ttu : Callable [[_KT , _VT , _TT ], _TT ],
95- timer : Callable [..., _TT ] = ...,
96- * ,
97- getsizeof : Callable [[_VT ], float ],
98- ): ...
65+ class TLRUCache (_TimedCache [_KT , _VT , _TT ]):
66+ def __init__ (self , maxsize : float , ttu : Callable [[_KT , _VT , _TT ], _TT ], timer : Callable [..., _TT ] = time .monotonic , getsizeof : Callable [[_VT ], float ] | None = None ): ...
9967 @property
10068 def ttu (self ) -> Callable [[_KT , _VT , _TT ], _TT ]: ...
101- def expire (self , time : Any | None = None ) -> list [tuple [_KT , _VT ]]: ...
69+ def expire (self , time : _TT | None = None ) -> list [tuple [_KT , _VT ]]: ...
10270
10371class _CacheInfo (NamedTuple ):
10472 hits : int
0 commit comments