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
5 changes: 5 additions & 0 deletions cachier/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ def _convert_args_kwargs(
func, _is_method: bool, args: tuple, kwds: dict
) -> dict:
"""Convert mix of positional and keyword arguments to aggregated kwargs."""
# unwrap if the function is functools.partial
if hasattr(func, "func"):
args = func.args + args
kwds = dict(**func.keywords, **kwds)
func = func.func
func_params = list(inspect.signature(func).parameters)
args_as_kw = dict(
zip(func_params[1:], args[1:])
Expand Down
3 changes: 3 additions & 0 deletions cachier/cores/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def set_func(self, func):
function is an object method.

"""
# unwrap if the function is functools.partial
if hasattr(func, "func"):
func = func.func
func_params = list(inspect.signature(func).parameters)
self.func_is_method = func_params and func_params[0] == "self"
self.func = func
Expand Down
47 changes: 45 additions & 2 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ def test_order_independent_kwargs_handling():
count = 0

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

dummy_func.clear_cache()
assert count == 0
Expand All @@ -333,3 +333,46 @@ def dummy_func(a, b=2):
dummy_func(a=1)
dummy_func(a=1, b=2)
assert count == 1


def test_runtime_handling(tmpdir):
count = 0

def dummy_func(a, b):
nonlocal count
count += 1
return a + b

cachier_ = cachier.cachier(cache_dir=tmpdir)
assert count == 0
cachier_(dummy_func)(a=1, b=2)
cachier_(dummy_func)(a=1, b=2)
assert count == 1


def test_partial_handling(tmpdir):
count = 0

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

cachier_ = cachier.cachier(cache_dir=tmpdir)
assert count == 0

dummy_ = functools.partial(dummy_func, 1)
cachier_(dummy_)()

dummy_ = functools.partial(dummy_func, a=1)
cachier_(dummy_)()

dummy_ = functools.partial(dummy_func, b=2)
cachier_(dummy_)(1)

assert count == 1

cachier_(dummy_func)(1, 2)
cachier_(dummy_func)(a=1, b=2)

assert count == 1