Skip to content

Commit 3a3f023

Browse files
authored
bux fix cache_kwargs (#129)
1 parent 69bae8a commit 3a3f023

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

cacholote/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
7070

7171
try:
7272
hexdigest = encode._hexdigestify_python_call(
73-
func, *args, **kwargs, **cache_kwargs
73+
func, *args, cache_kwargs=cache_kwargs, **kwargs
7474
)
7575
except encode.EncodeError as ex:
7676
if settings.return_cache_entry:

cacholote/encode.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333
def _hexdigestify_python_call(
3434
func_to_hex: str | Callable[..., Any],
3535
*args: Any,
36+
cache_kwargs: dict[str, Any] = {},
3637
**kwargs: Any,
3738
) -> str:
38-
return utils.hexdigestify(dumps_python_call(func_to_hex, *args, **kwargs))
39+
return utils.hexdigestify(
40+
dumps_python_call(func_to_hex, *args, cache_kwargs=cache_kwargs, **kwargs)
41+
)
3942

4043

4144
def inspect_fully_qualified_name(obj: Callable[..., Any]) -> str:
@@ -63,6 +66,7 @@ def dictify_python_object(obj: str | Callable[..., Any]) -> dict[str, str]:
6366
def dictify_python_call(
6467
func_to_dict: str | Callable[..., Any],
6568
*args: Any,
69+
cache_kwargs: dict[str, Any] = {},
6670
**kwargs: Any,
6771
) -> dict[str, Any]:
6872
callable_fqn = dictify_python_object(func_to_dict)["fully_qualified_name"]
@@ -89,6 +93,8 @@ def dictify_python_call(
8993
python_call_simple["args"] = args
9094
if kwargs:
9195
python_call_simple["kwargs"] = {k: kwargs[k] for k in sorted(kwargs)}
96+
if cache_kwargs:
97+
python_call_simple["cache_kwargs"] = dict(sorted(cache_kwargs.items()))
9298

9399
return python_call_simple
94100

@@ -189,6 +195,7 @@ def dumps(
189195
def dumps_python_call(
190196
func_to_dump: str | Callable[..., Any],
191197
*args: Any,
198+
cache_kwargs: dict[str, Any] = {},
192199
**kwargs: Any,
193200
) -> str:
194201
"""Serialize python call to JSON formatted string.
@@ -199,12 +206,16 @@ def dumps_python_call(
199206
Function to serialize
200207
*args: Any
201208
Arguments of ``func``
209+
cache_kwargs: dict
210+
Additional arguments for hashing only
202211
**kwargs: Any
203212
Keyword arguments of ``func``
204213
205214
Returns
206215
-------
207216
str
208217
"""
209-
python_call = dictify_python_call(func_to_dump, *args, **kwargs)
218+
python_call = dictify_python_call(
219+
func_to_dump, *args, cache_kwargs=cache_kwargs, **kwargs
220+
)
210221
return dumps(python_call)

tests/test_30_cache.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,22 @@ def cached_error() -> None:
3030
raise ValueError("test error")
3131

3232

33+
def test_cache_kwargs() -> None:
34+
def test_func() -> datetime.datetime:
35+
return datetime.datetime.now()
36+
37+
func1 = cache.cacheable(test_func, count=1)
38+
func2 = cache.cacheable(test_func, count=2)
39+
assert func1() != func2()
40+
assert func1() == func1()
41+
assert func2() == func2()
42+
43+
3344
@pytest.mark.parametrize(
3445
"cache_kwargs,expected_hash",
3546
[
3647
({}, "a8260ac3cdc1404aa64a6fb71e853049"),
37-
({"foo": "bar"}, "ad4c1867757974cfccabc18c3a5078b9"),
48+
({"foo": "bar"}, "f732a8c299b41e9d73b7bf1d32a2fa68"),
3849
],
3950
)
4051
def test_cacheable(cache_kwargs: dict[str, Any], expected_hash: str) -> None:

0 commit comments

Comments
 (0)