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
14 changes: 11 additions & 3 deletions cachier/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import datetime
import functools
import hashlib
import inspect
import os
import pickle
from collections import OrderedDict
Expand Down Expand Up @@ -94,13 +95,20 @@ def _convert_args_kwargs(
func, _is_method: bool, args: tuple, kwds: dict
) -> dict:
"""Convert mix of positional and keyword arguments to aggregated kwargs."""
func_params = list(inspect.signature(func).parameters)
args_as_kw = dict(
zip(func.__code__.co_varnames[1:], args[1:])
zip(func_params[1:], args[1:])
if _is_method
else zip(func.__code__.co_varnames, args)
else zip(func_params, args)
)
# init with default values
kwargs = {
k: v.default
for k, v in inspect.signature(func).parameters.items()
if v.default is not inspect.Parameter.empty
}
# merge args expanded as kwargs and the original kwds
kwargs = dict(**args_as_kw, **kwds)
kwargs.update(dict(**args_as_kw, **kwds))
return OrderedDict(sorted(kwargs.items()))


Expand Down
17 changes: 17 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,20 @@ def dummy_func(a=None, b=None):
assert count == 1
dummy_func(b=2, a=1)
assert count == 1


def test_default_kwargs_handling():
count = 0

@cachier.cachier()
def dummy_func(a, b=2):
nonlocal count
count += 1
return a + b

dummy_func.clear_cache()
assert count == 0
dummy_func(1)
dummy_func(a=1)
dummy_func(a=1, b=2)
assert count == 1