Skip to content

Commit 4432b14

Browse files
fix(cache): support for unpassed default args (#39)
* feat(cache): support caching functions with positional-only arguments BREAKING CHANGE: requires Python version >= 3.8 * fix(cache): cache params with defaults not passed as args --------- Co-authored-by: Taylor Hakes <[email protected]>
1 parent e701a9e commit 4432b14

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

redis_cache/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from functools import wraps
22
from json import dumps, loads
33
from base64 import b64encode
4-
from inspect import signature
4+
from inspect import signature, Parameter
55

66
def compact_dump(value):
77
return dumps(value, separators=(',', ':'), sort_keys=True)
@@ -44,6 +44,10 @@ def get_args(fn, args, kwargs):
4444
parsed_args[vkwargs_name] = {}
4545
parsed_args[vkwargs_name][key] = value
4646

47+
for param in arg_sig.parameters.values():
48+
if param.name not in parsed_args and param.default is not Parameter.empty:
49+
parsed_args[param.name] = param.default
50+
4751
return parsed_args
4852

4953

tests/test_redis_cache.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ def fn5(*, d, **e):
365365
def fn6(a, b, /, c, d):
366366
pass
367367

368+
def fn7(a, b, c=3, *, d):
369+
pass
370+
368371
assert get_args(fn1, (1,2), {}) == dict(a=1, b=2)
369372
assert get_args(fn1, [], dict(a=1, b=2)) == dict(a=1, b=2)
370373
assert get_args(fn1, [1], dict(b=2)) == dict(a=1, b=2)
@@ -373,6 +376,7 @@ def fn6(a, b, /, c, d):
373376
assert get_args(fn4, [1, 2, 3, 4], dict(d=5, f=6, g=7, h=8)) == dict(a=1, c=[2, 3, 4], d=5, e=dict(f=6, g=7, h=8))
374377
assert get_args(fn5, [], dict(d=5, f=6, g=7, h=8)) == dict(d=5, e=dict(f=6, g=7, h=8))
375378
assert get_args(fn6, [1, 2, 3], dict(d=4)) == dict(a=1, b=2, c=3, d=4)
379+
assert get_args(fn7, [1, 2], dict(d=4)) == dict(a=1, b=2, c=3, d=4)
376380

377381
# Simulate the environment where redis is not available
378382
# Only test the CacheDecorator since the exception handling should be done inside the decorator

0 commit comments

Comments
 (0)