Skip to content

Commit

Permalink
Include defaults in cachable key
Browse files Browse the repository at this point in the history
  • Loading branch information
frthjf committed Dec 6, 2024
1 parent 7b45037 commit f8e7382
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Unreleased

- Support sqlean.py
- Include defaults in cachable key

# v4.10.5

Expand Down
9 changes: 8 additions & 1 deletion src/machinable/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ def _wrapper(self, *args, **kwargs):
if not self.cached():
return fn(self, *args, **kwargs)
try:
sig = inspect.signature(fn)
bound_args = sig.bind(self, *args, **kwargs)
bound_args.apply_defaults()
key = object_hash(
{"fn": fn.__name__, "args": args, "kwargs": kwargs}
{
"fn": fn.__name__,
"args": bound_args.args[1:],
"kwargs": bound_args.kwargs,
}
)[:8]
except TypeError as _ex:
if fail_mode == "raise":
Expand Down
14 changes: 14 additions & 0 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,20 @@ def test3(self, a=0, k="test"):
assert c.test3(k=slice(1)) == 700
assert c.test3(k=slice(1)) == 800

# default-args
class C(Component):
@cachable()
def test(self, payload="default", a=1):
return getattr(self, "latent", "test")

c = C().launch()
assert c.test(a=1) == "test"
c.latent = "changed"
assert c.test(a=1) == "test"
assert c.test(payload="default", a=1) == "test"
assert c.test(payload="default") == "test"
assert c.test(a=2) == "changed"


def test_interface_update(tmp_storage):
i = get("machinable.interface", {"a": 1}).commit()
Expand Down

0 comments on commit f8e7382

Please sign in to comment.