11from __future__ import annotations
22
3+ from collections .abc import Callable
4+ from collections .abc import Generator
5+ from collections .abc import Iterable
6+ from collections .abc import Mapping
7+ from collections .abc import Sequence
38import contextlib
49import datetime
510from functools import partial
1015import threading
1116import time
1217from typing import Any
13- from typing import Callable
1418from typing import cast
15- from typing import Mapping
1619from typing import Optional
17- from typing import Sequence
18- from typing import Tuple
1920from typing import Type
2021from typing import TYPE_CHECKING
2122from typing import Union
6465 ["CacheRegion" , KeyType , Callable [[], ValuePayload ], CacheMutex ], None
6566]
6667
67- ExpirationTimeCallable = Callable [[], float ]
68+ ExpirationTimeCallable = Callable [[], Optional [ float ] ]
6869
6970ToStr = Callable [[Any ], str ]
7071
@@ -648,7 +649,9 @@ def invalidate(self, hard=True):
648649 """
649650 self .region_invalidator .invalidate (hard )
650651
651- def configure_from_config (self , config_dict , prefix ):
652+ def configure_from_config (
653+ self , config_dict : dict [str , Any ], prefix : str
654+ ) -> Self :
652655 """Configure from a configuration dictionary
653656 and a prefix.
654657
@@ -680,7 +683,7 @@ def configure_from_config(self, config_dict, prefix):
680683 ),
681684 _config_argument_dict = config_dict ,
682685 _config_prefix = "%sarguments." % prefix ,
683- wrap = config_dict .get ("%swrap" % prefix , None ),
686+ wrap = config_dict .get ("%swrap" % prefix , None ), # type: ignore
684687 replace_existing_backend = config_dict .get (
685688 "%sreplace_existing_backend" % prefix , False
686689 ),
@@ -693,7 +696,7 @@ def backend(self):
693696 )
694697
695698 @property
696- def is_configured (self ):
699+ def is_configured (self ) -> bool :
697700 """Return True if the backend has been configured via the
698701 :meth:`.CacheRegion.configure` method already.
699702
@@ -902,7 +905,7 @@ def get_multi(self, keys, expiration_time=None, ignore_expiration=False):
902905 ]
903906
904907 @contextlib .contextmanager
905- def _log_time (self , keys ) :
908+ def _log_time (self , keys : Iterable [ str ]) -> Generator [ None , None , None ] :
906909 start_time = time .time ()
907910 yield
908911 seconds = time .time () - start_time
@@ -941,7 +944,7 @@ def get_or_create(
941944 creator : Callable [..., ValuePayload ],
942945 expiration_time : Optional [float ] = None ,
943946 should_cache_fn : Optional [Callable [[ValuePayload ], bool ]] = None ,
944- creator_args : Optional [Tuple [Any , Mapping [str , Any ]]] = None ,
947+ creator_args : Optional [tuple [Any , Mapping [str , Any ]]] = None ,
945948 ) -> ValuePayload :
946949 """Return a cached value based on the given key.
947950
@@ -1104,7 +1107,7 @@ def go():
11041107
11051108 def get_or_create_multi (
11061109 self ,
1107- keys : Sequence [KeyType ],
1110+ keys : Iterable [KeyType ],
11081111 creator : Callable [[], ValuePayload ],
11091112 expiration_time : Optional [float ] = None ,
11101113 should_cache_fn : Optional [Callable [[ValuePayload ], bool ]] = None ,
@@ -1327,7 +1330,7 @@ def _get_from_backend(self, key: KeyType) -> CacheReturnType:
13271330 return cast (CacheReturnType , self .backend .get (key ))
13281331
13291332 def _get_multi_from_backend (
1330- self , keys : Sequence [KeyType ]
1333+ self , keys : Iterable [KeyType ]
13311334 ) -> Sequence [CacheReturnType ]:
13321335 if self .deserializer :
13331336 return [
@@ -1425,7 +1428,7 @@ def delete(self, key: KeyType) -> None:
14251428
14261429 self .backend .delete (key )
14271430
1428- def delete_multi (self , keys : Sequence [KeyType ]) -> None :
1431+ def delete_multi (self , keys : Iterable [KeyType ]) -> None :
14291432 """Remove multiple values from the cache.
14301433
14311434 This operation is idempotent (can be called multiple times, or on a
@@ -1640,9 +1643,7 @@ def get_or_create_for_user_func(key_generator, user_func, *arg, **kw):
16401643 def cache_decorator (user_func ):
16411644 if to_str is cast (Callable [[Any ], str ], str ):
16421645 # backwards compatible
1643- key_generator = _function_key_generator (
1644- namespace , user_func
1645- ) # type: ignore
1646+ key_generator = _function_key_generator (namespace , user_func )
16461647 else :
16471648 key_generator = _function_key_generator (
16481649 namespace , user_func , to_str
0 commit comments