From 193d9ac4feccd21dfe1500c75ac121c4afefd65c Mon Sep 17 00:00:00 2001 From: Krukov Date: Wed, 23 Oct 2024 00:19:44 +0300 Subject: [PATCH] fix get_or_set for coro default --- cashews/wrapper/commands.py | 4 +++- tests/test_backend_commands.py | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cashews/wrapper/commands.py b/cashews/wrapper/commands.py index 4ad076b..ad23c79 100644 --- a/cashews/wrapper/commands.py +++ b/cashews/wrapper/commands.py @@ -49,7 +49,9 @@ async def get_or_set( value = await self.get(key, default=_empty) if value is not _empty: return value - if callable(default): + if inspect.isawaitable(default): + _default = await default + elif callable(default): if inspect.iscoroutinefunction(default): _default = await default() else: diff --git a/tests/test_backend_commands.py b/tests/test_backend_commands.py index 92cbc46..ac1eee4 100644 --- a/tests/test_backend_commands.py +++ b/tests/test_backend_commands.py @@ -33,6 +33,14 @@ async def _default(): assert await cache.get("key") == VALUE +async def test_get_or_set_coroutine(cache: Cache): + async def _default(): + return VALUE + + await cache.get_or_set("key", default=_default()) + assert await cache.get("key") == VALUE + + async def test_get_or_set_no_set(cache: Cache): await cache.set("key", VALUE) await cache.get_or_set("key", None)