Skip to content

Commit b469bfe

Browse files
authored
Fix default arguments are not respected when crafting cache key (#172)
2 parents 7e6b892 + 86c1817 commit b469bfe

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

py_cachify/_backend/_helpers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414

1515

1616
def get_full_key_from_signature(bound_args: inspect.BoundArguments, key: str) -> str:
17+
bound_args.apply_defaults()
18+
_args_repr = f'{bound_args}'
19+
1720
args_dict = bound_args.arguments
18-
args: Tuple[str, Any] = args_dict.pop('args', ())
21+
args: Tuple[Any, ...] = args_dict.pop('args', ())
1922
kwargs: Dict[str, Any] = args_dict.pop('kwargs', {})
2023
kwargs.update(args_dict)
2124

2225
try:
2326
return key.format(*args, **kwargs)
24-
except IndexError:
25-
raise ValueError('Arguments in a key do not match function signature') from None
27+
except (IndexError, KeyError):
28+
raise ValueError(f'Arguments in a key({key}) do not match function signature params({_args_repr})') from None
2629

2730

2831
def is_coroutine(

tests/test_helpers.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
import re
23

34
import pytest
45
from pytest_mock import MockerFixture
@@ -23,7 +24,12 @@ def test_get_full_key_valid_arguments(args_kwargs_signature):
2324

2425
def test_get_full_key_invalid_key_format(args_kwargs_signature):
2526
bound_args = args_kwargs_signature.bind('value1', 'value2')
26-
with pytest.raises(ValueError, match='Arguments in a key do not match function signature'):
27+
bound_args.apply_defaults()
28+
29+
with pytest.raises(
30+
ValueError,
31+
match=re.escape(f'Arguments in a key(key_{{}}_{{}}_{{}}) do not match function signature params({bound_args})'),
32+
):
2733
get_full_key_from_signature(bound_args, 'key_{}_{}_{}')
2834

2935

@@ -35,8 +41,16 @@ def test_get_full_key_empty_key_and_arguments(args_kwargs_signature):
3541

3642
def test_get_full_key_mixed_placeholders(args_kwargs_signature):
3743
bound_args = args_kwargs_signature.bind('value1', 'value2', arg3='value3')
38-
with pytest.raises(ValueError, match='Arguments in a key do not match function signature'):
39-
get_full_key_from_signature(bound_args, 'key_{}_{}_{}_{invalid_arg}')
44+
bound_args.apply_defaults()
45+
46+
with pytest.raises(
47+
ValueError,
48+
match=re.escape(
49+
'Arguments in a key(key_{}_{}_{}_{invalid_arg}) '
50+
+ f'do not match function signature params({bound_args})'
51+
),
52+
):
53+
_ = get_full_key_from_signature(bound_args, 'key_{}_{}_{}_{invalid_arg}')
4054

4155

4256
def test_reset_calls_delete_with_key(init_cachify_fixture, args_kwargs_signature, mocker: MockerFixture):

0 commit comments

Comments
 (0)