Skip to content

Commit

Permalink
Merge pull request #295 from Krukov/get_or_set_with_coro_as_default
Browse files Browse the repository at this point in the history
fix get_or_set for coro default
  • Loading branch information
Krukov authored Oct 22, 2024
2 parents e2bd542 + 193d9ac commit 6d9c487
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cashews/wrapper/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_backend_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6d9c487

Please sign in to comment.