Skip to content

Commit 38b0750

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 4d52816 commit 38b0750

1 file changed

Lines changed: 15 additions & 50 deletions

File tree

src/cachier/core.py

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ 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(
69-
core: _BaseCore, key, func, args, kwds, printer=lambda *_: None
70-
) -> Optional[Any]:
68+
def _calc_entry(core: _BaseCore, key, func, args, kwds, printer=lambda *_: None) -> Optional[Any]:
7169
core.mark_entry_being_calculated(key)
7270
try:
7371
func_res = func(*args, **kwds)
@@ -79,9 +77,7 @@ def _calc_entry(
7977
core.mark_entry_not_calculated(key)
8078

8179

82-
async def _calc_entry_async(
83-
core: _BaseCore, key, func, args, kwds, printer=lambda *_: None
84-
) -> Optional[Any]:
80+
async def _calc_entry_async(core: _BaseCore, key, func, args, kwds, printer=lambda *_: None) -> Optional[Any]:
8581
await core.amark_entry_being_calculated(key)
8682
try:
8783
func_res = await func(*args, **kwds)
@@ -139,11 +135,7 @@ def _convert_args_kwargs(func, _is_method: bool, args: tuple, kwds: dict) -> dic
139135
args_as_kw[f"__varargs_{i}__"] = arg
140136

141137
# Init with default values
142-
kwargs = {
143-
k: v.default
144-
for k, v in sig.parameters.items()
145-
if v.default is not inspect.Parameter.empty
146-
}
138+
kwargs = {k: v.default for k, v in sig.parameters.items() if v.default is not inspect.Parameter.empty}
147139

148140
# Merge args expanded as kwargs and the original kwds
149141
kwargs.update(args_as_kw)
@@ -168,10 +160,7 @@ def _is_async_redis_client(client: Any) -> bool:
168160
if client is None:
169161
return False
170162
method_names = ("hgetall", "hset", "keys", "delete", "hget")
171-
return all(
172-
inspect.iscoroutinefunction(getattr(client, name, None))
173-
for name in method_names
174-
)
163+
return all(inspect.iscoroutinefunction(getattr(client, name, None)) for name in method_names)
175164

176165

177166
def cachier(
@@ -274,9 +263,7 @@ def cachier(
274263
# Update parameters with defaults if input is None
275264
backend = _update_with_defaults(backend, "backend")
276265
mongetter = _update_with_defaults(mongetter, "mongetter")
277-
size_limit_bytes = parse_bytes(
278-
_update_with_defaults(entry_size_limit, "entry_size_limit")
279-
)
266+
size_limit_bytes = parse_bytes(_update_with_defaults(entry_size_limit, "entry_size_limit"))
280267
# Override the backend parameter if a mongetter is provided.
281268
if callable(mongetter):
282269
backend = "mongo"
@@ -343,9 +330,7 @@ def _cachier_decorator(func):
343330
raise TypeError(msg)
344331
else:
345332
if callable(redis_client) and inspect.iscoroutinefunction(redis_client):
346-
msg = (
347-
"Async redis_client callable requires an async cached function."
348-
)
333+
msg = "Async redis_client callable requires an async cached function."
349334
raise TypeError(msg)
350335
if _is_async_redis_client(redis_client):
351336
msg = "Async Redis client requires an async cached function."
@@ -401,13 +386,9 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
401386
_stale_after = _update_with_defaults(stale_after, "stale_after", kwds)
402387
_next_time = _update_with_defaults(next_time, "next_time", kwds)
403388
_cleanup_flag = _update_with_defaults(cleanup_stale, "cleanup_stale", kwds)
404-
_cleanup_interval_val = _update_with_defaults(
405-
cleanup_interval, "cleanup_interval", kwds
406-
)
389+
_cleanup_interval_val = _update_with_defaults(cleanup_interval, "cleanup_interval", kwds)
407390
# merge args expanded as kwargs and the original kwds
408-
kwargs = _convert_args_kwargs(
409-
func, _is_method=core.func_is_method, args=args, kwds=kwds
410-
)
391+
kwargs = _convert_args_kwargs(func, _is_method=core.func_is_method, args=args, kwds=kwds)
411392

412393
if _cleanup_flag:
413394
now = datetime.now()
@@ -422,9 +403,7 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
422403
from .config import _global_params
423404

424405
if ignore_cache or not _global_params.caching_enabled:
425-
return (
426-
func(args[0], **kwargs) if core.func_is_method else func(**kwargs)
427-
)
406+
return func(args[0], **kwargs) if core.func_is_method else func(**kwargs)
428407
key, entry = core.get_entry((), kwargs)
429408
if overwrite_cache:
430409
return _calc_entry(core, key, func, args, kwds, _print)
@@ -462,9 +441,7 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
462441
_print("Async calc and return stale")
463442
core.mark_entry_being_calculated(key)
464443
try:
465-
_get_executor().submit(
466-
_function_thread, core, key, func, args, kwds
467-
)
444+
_get_executor().submit(_function_thread, core, key, func, args, kwds)
468445
finally:
469446
core.mark_entry_not_calculated(key)
470447
return entry.value
@@ -496,13 +473,9 @@ async def _call_async(*args, max_age: Optional[timedelta] = None, **kwds):
496473
_stale_after = _update_with_defaults(stale_after, "stale_after", kwds)
497474
_next_time = _update_with_defaults(next_time, "next_time", kwds)
498475
_cleanup_flag = _update_with_defaults(cleanup_stale, "cleanup_stale", kwds)
499-
_cleanup_interval_val = _update_with_defaults(
500-
cleanup_interval, "cleanup_interval", kwds
501-
)
476+
_cleanup_interval_val = _update_with_defaults(cleanup_interval, "cleanup_interval", kwds)
502477
# merge args expanded as kwargs and the original kwds
503-
kwargs = _convert_args_kwargs(
504-
func, _is_method=core.func_is_method, args=args, kwds=kwds
505-
)
478+
kwargs = _convert_args_kwargs(func, _is_method=core.func_is_method, args=args, kwds=kwds)
506479

507480
if _cleanup_flag:
508481
now = datetime.now()
@@ -517,11 +490,7 @@ async def _call_async(*args, max_age: Optional[timedelta] = None, **kwds):
517490
from .config import _global_params
518491

519492
if ignore_cache or not _global_params.caching_enabled:
520-
return (
521-
await func(args[0], **kwargs)
522-
if core.func_is_method
523-
else await func(**kwargs)
524-
)
493+
return await func(args[0], **kwargs) if core.func_is_method else await func(**kwargs)
525494
key, entry = await core.aget_entry((), kwargs)
526495
if overwrite_cache:
527496
result = await _calc_entry_async(core, key, func, args, kwds, _print)
@@ -555,9 +524,7 @@ async def _call_async(*args, max_age: Optional[timedelta] = None, **kwds):
555524
# Background task will update cache when complete
556525
await core.amark_entry_being_calculated(key)
557526
# Use asyncio.create_task for background execution
558-
asyncio.create_task(
559-
_function_thread_async(core, key, func, args, kwds)
560-
)
527+
asyncio.create_task(_function_thread_async(core, key, func, args, kwds))
561528
await core.amark_entry_not_calculated(key)
562529
return entry.value
563530
_print("Calling decorated function and waiting")
@@ -621,9 +588,7 @@ def _precache_value(*args, value_to_cache, **kwds): # noqa: D417
621588
622589
"""
623590
# merge args expanded as kwargs and the original kwds
624-
kwargs = _convert_args_kwargs(
625-
func, _is_method=core.func_is_method, args=args, kwds=kwds
626-
)
591+
kwargs = _convert_args_kwargs(func, _is_method=core.func_is_method, args=args, kwds=kwds)
627592
return core.precache_value((), kwargs, value_to_cache)
628593

629594
func_wrapper.clear_cache = _clear_cache

0 commit comments

Comments
 (0)