@@ -65,7 +65,9 @@ async def _function_thread_async(core: _BaseCore, key, func, args, kwds):
6565 print (f"Function call failed with the following exception:\n { exc } " )
6666
6767
68- def _calc_entry (core : _BaseCore , key , func , args , kwds , printer = lambda * _ : None ) -> Optional [Any ]:
68+ def _calc_entry (
69+ core : _BaseCore , key , func , args , kwds , printer = lambda * _ : None
70+ ) -> Optional [Any ]:
6971 core .mark_entry_being_calculated (key )
7072 try :
7173 func_res = func (* args , ** kwds )
@@ -77,7 +79,9 @@ def _calc_entry(core: _BaseCore, key, func, args, kwds, printer=lambda *_: None)
7779 core .mark_entry_not_calculated (key )
7880
7981
80- async def _calc_entry_async (core : _BaseCore , key , func , args , kwds , printer = lambda * _ : None ) -> Optional [Any ]:
82+ async def _calc_entry_async (
83+ core : _BaseCore , key , func , args , kwds , printer = lambda * _ : None
84+ ) -> Optional [Any ]:
8185 await core .amark_entry_being_calculated (key )
8286 try :
8387 func_res = await func (* args , ** kwds )
@@ -125,7 +129,7 @@ def _convert_args_kwargs(func, _is_method: bool, args: tuple, kwds: dict) -> dic
125129
126130 # Map as many args as possible to regular parameters
127131 num_regular = len (params_to_use )
128- args_as_kw = dict (zip (params_to_use , args_to_map [:num_regular ], strict = False ))
132+ args_as_kw = dict (zip (params_to_use , args_to_map [:num_regular ]))
129133
130134 # Handle variadic positional arguments
131135 # Store them with indexed keys like __varargs_0__, __varargs_1__, etc.
@@ -135,7 +139,11 @@ def _convert_args_kwargs(func, _is_method: bool, args: tuple, kwds: dict) -> dic
135139 args_as_kw [f"__varargs_{ i } __" ] = arg
136140
137141 # Init with default values
138- kwargs = {k : v .default for k , v in sig .parameters .items () if v .default is not inspect .Parameter .empty }
142+ kwargs = {
143+ k : v .default
144+ for k , v in sig .parameters .items ()
145+ if v .default is not inspect .Parameter .empty
146+ }
139147
140148 # Merge args expanded as kwargs and the original kwds
141149 kwargs .update (args_as_kw )
@@ -160,7 +168,10 @@ def _is_async_redis_client(client: Any) -> bool:
160168 if client is None :
161169 return False
162170 method_names = ("hgetall" , "hset" , "keys" , "delete" , "hget" )
163- return all (inspect .iscoroutinefunction (getattr (client , name , None )) for name in method_names )
171+ return all (
172+ inspect .iscoroutinefunction (getattr (client , name , None ))
173+ for name in method_names
174+ )
164175
165176
166177def cachier (
@@ -263,7 +274,9 @@ def cachier(
263274 # Update parameters with defaults if input is None
264275 backend = _update_with_defaults (backend , "backend" )
265276 mongetter = _update_with_defaults (mongetter , "mongetter" )
266- size_limit_bytes = parse_bytes (_update_with_defaults (entry_size_limit , "entry_size_limit" ))
277+ size_limit_bytes = parse_bytes (
278+ _update_with_defaults (entry_size_limit , "entry_size_limit" )
279+ )
267280 # Override the backend parameter if a mongetter is provided.
268281 if callable (mongetter ):
269282 backend = "mongo"
@@ -286,7 +299,9 @@ def cachier(
286299 )
287300 elif backend == "memory" :
288301 core = _MemoryCore (
289- hash_func = hash_func , wait_for_calc_timeout = wait_for_calc_timeout , entry_size_limit = size_limit_bytes
302+ hash_func = hash_func ,
303+ wait_for_calc_timeout = wait_for_calc_timeout ,
304+ entry_size_limit = size_limit_bytes ,
290305 )
291306 elif backend == "sql" :
292307 core = _SQLCore (
@@ -328,7 +343,9 @@ def _cachier_decorator(func):
328343 raise TypeError (msg )
329344 else :
330345 if callable (redis_client ) and inspect .iscoroutinefunction (redis_client ):
331- msg = "Async redis_client callable requires an async cached function."
346+ msg = (
347+ "Async redis_client callable requires an async cached function."
348+ )
332349 raise TypeError (msg )
333350 if _is_async_redis_client (redis_client ):
334351 msg = "Async Redis client requires an async cached function."
@@ -384,9 +401,13 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
384401 _stale_after = _update_with_defaults (stale_after , "stale_after" , kwds )
385402 _next_time = _update_with_defaults (next_time , "next_time" , kwds )
386403 _cleanup_flag = _update_with_defaults (cleanup_stale , "cleanup_stale" , kwds )
387- _cleanup_interval_val = _update_with_defaults (cleanup_interval , "cleanup_interval" , kwds )
404+ _cleanup_interval_val = _update_with_defaults (
405+ cleanup_interval , "cleanup_interval" , kwds
406+ )
388407 # merge args expanded as kwargs and the original kwds
389- kwargs = _convert_args_kwargs (func , _is_method = core .func_is_method , args = args , kwds = kwds )
408+ kwargs = _convert_args_kwargs (
409+ func , _is_method = core .func_is_method , args = args , kwds = kwds
410+ )
390411
391412 if _cleanup_flag :
392413 now = datetime .now ()
@@ -401,7 +422,9 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
401422 from .config import _global_params
402423
403424 if ignore_cache or not _global_params .caching_enabled :
404- return func (args [0 ], ** kwargs ) if core .func_is_method else func (** kwargs )
425+ return (
426+ func (args [0 ], ** kwargs ) if core .func_is_method else func (** kwargs )
427+ )
405428 key , entry = core .get_entry ((), kwargs )
406429 if overwrite_cache :
407430 return _calc_entry (core , key , func , args , kwds , _print )
@@ -439,7 +462,9 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
439462 _print ("Async calc and return stale" )
440463 core .mark_entry_being_calculated (key )
441464 try :
442- _get_executor ().submit (_function_thread , core , key , func , args , kwds )
465+ _get_executor ().submit (
466+ _function_thread , core , key , func , args , kwds
467+ )
443468 finally :
444469 core .mark_entry_not_calculated (key )
445470 return entry .value
@@ -471,9 +496,13 @@ async def _call_async(*args, max_age: Optional[timedelta] = None, **kwds):
471496 _stale_after = _update_with_defaults (stale_after , "stale_after" , kwds )
472497 _next_time = _update_with_defaults (next_time , "next_time" , kwds )
473498 _cleanup_flag = _update_with_defaults (cleanup_stale , "cleanup_stale" , kwds )
474- _cleanup_interval_val = _update_with_defaults (cleanup_interval , "cleanup_interval" , kwds )
499+ _cleanup_interval_val = _update_with_defaults (
500+ cleanup_interval , "cleanup_interval" , kwds
501+ )
475502 # merge args expanded as kwargs and the original kwds
476- kwargs = _convert_args_kwargs (func , _is_method = core .func_is_method , args = args , kwds = kwds )
503+ kwargs = _convert_args_kwargs (
504+ func , _is_method = core .func_is_method , args = args , kwds = kwds
505+ )
477506
478507 if _cleanup_flag :
479508 now = datetime .now ()
@@ -488,7 +517,11 @@ async def _call_async(*args, max_age: Optional[timedelta] = None, **kwds):
488517 from .config import _global_params
489518
490519 if ignore_cache or not _global_params .caching_enabled :
491- return await func (args [0 ], ** kwargs ) if core .func_is_method else await func (** kwargs )
520+ return (
521+ await func (args [0 ], ** kwargs )
522+ if core .func_is_method
523+ else await func (** kwargs )
524+ )
492525 key , entry = await core .aget_entry ((), kwargs )
493526 if overwrite_cache :
494527 result = await _calc_entry_async (core , key , func , args , kwds , _print )
@@ -522,7 +555,9 @@ async def _call_async(*args, max_age: Optional[timedelta] = None, **kwds):
522555 # Background task will update cache when complete
523556 await core .amark_entry_being_calculated (key )
524557 # Use asyncio.create_task for background execution
525- asyncio .create_task (_function_thread_async (core , key , func , args , kwds ))
558+ asyncio .create_task (
559+ _function_thread_async (core , key , func , args , kwds )
560+ )
526561 await core .amark_entry_not_calculated (key )
527562 return entry .value
528563 _print ("Calling decorated function and waiting" )
@@ -549,6 +584,7 @@ async def _call_async(*args, max_age: Optional[timedelta] = None, **kwds):
549584 @wraps (func )
550585 async def func_wrapper (* args , ** kwargs ):
551586 return await _call_async (* args , ** kwargs )
587+
552588 else :
553589
554590 @wraps (func )
@@ -585,7 +621,9 @@ def _precache_value(*args, value_to_cache, **kwds): # noqa: D417
585621
586622 """
587623 # merge args expanded as kwargs and the original kwds
588- kwargs = _convert_args_kwargs (func , _is_method = core .func_is_method , args = args , kwds = kwds )
624+ kwargs = _convert_args_kwargs (
625+ func , _is_method = core .func_is_method , args = args , kwds = kwds
626+ )
589627 return core .precache_value ((), kwargs , value_to_cache )
590628
591629 func_wrapper .clear_cache = _clear_cache
0 commit comments