Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.10.0
current_version = 1.10.1
commit = False
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+)(?P<build>\d+))?
Expand Down
2 changes: 1 addition & 1 deletion nwastdlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#
"""The NWA-stdlib module."""

__version__ = "1.10.0"
__version__ = "1.10.1"

from nwastdlib.f import const, identity

Expand Down
10 changes: 6 additions & 4 deletions nwastdlib/asyncio_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@
return _deserialize(pickled_value, serializer)


def _generate_cache_key_suffix(*, skip_first: bool, args: tuple, kwargs: dict) -> str:
def _generate_cache_key_suffix(*, skip_first: bool, args: tuple, kwargs: dict) -> str | None:
# Auto generate cache key suffix based on the arguments
# Note: this makes no attempt to handle non-hashable values like lists and sets or other complex objects
filtered_args = args[int(skip_first) :]
filtered_kwargs = frozenset(kwargs.items())
if not filtered_args and not filtered_kwargs:
raise ValueError("Cannot generate cache key without args/kwargs")
return None
args_and_kwargs_string = (filtered_args, filtered_kwargs)
return str(args_and_kwargs_string)

Expand Down Expand Up @@ -244,16 +244,18 @@
def cache_decorator(func: Callable) -> Callable:
_validate_coroutine(func)
skip_first = _validate_signature(func)
prefix_version_func = f"{prefix_version}:{func.__qualname__}".lower()

@wraps(func)
async def func_wrapper(*args: tuple[Any], **kwargs: dict[str, Any]) -> Any:
from_cache = (not revalidate_fn(*args, **kwargs)) if revalidate_fn else True

if static_cache_key:
cache_key = static_cache_key
elif suffix := _generate_cache_key_suffix(skip_first=skip_first, args=args, kwargs=kwargs):
cache_key = f"{prefix_version_func}:{suffix}"
else:
suffix = _generate_cache_key_suffix(skip_first=skip_first, args=args, kwargs=kwargs)
cache_key = f"{prefix_version}:{func.__name__}:{suffix}"
cache_key = prefix_version_func

Check warning on line 258 in nwastdlib/asyncio_cache.py

View check run for this annotation

Codecov / codecov/patch

nwastdlib/asyncio_cache.py#L258

Added line #L258 was not covered by tests

if from_cache:
logger.debug("Cache called with wrapper func", func_name=func.__name__, cache_key=cache_key)
Expand Down
12 changes: 1 addition & 11 deletions tests/test_asyncio_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,18 +306,8 @@ async def foo(param: str):
{},
"((UUID('12345678-0000-1111-2222-0123456789ab'),), frozenset())",
),
(False, (), {}, None),
],
)
def test_generate_cache_key_suffix(skip_first, args, kwargs, expected_key):
assert _generate_cache_key_suffix(skip_first=skip_first, args=args, kwargs=kwargs) == expected_key


@pytest.mark.parametrize(
("skip_first", "args", "kwargs", "expected_exception"),
[
(False, (), {}, ValueError),
],
)
def test_generate_cache_key_errors(skip_first, args, kwargs, expected_exception):
with pytest.raises(expected_exception):
_generate_cache_key_suffix(skip_first=skip_first, args=args, kwargs=kwargs)