Skip to content

Commit d52050b

Browse files
committed
more general cached_member_function decorator
1 parent 340c368 commit d52050b

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

adcc/misc.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
## ---------------------------------------------------------------------
2323
import warnings
2424
import numpy as np
25+
import inspect
2526
from functools import wraps
2627
from pkg_resources import parse_version
2728

@@ -58,8 +59,26 @@ def cached_member_function(function):
5859
"""
5960
fname = function.__name__
6061

62+
# get the function signature and ensure that we don't have any
63+
# keyword only arguments:
64+
# func(..., *, kwarg=None, ...) or func(..., **kwargs).
65+
# If we want to support them we need to add them in a well defined
66+
# order to the cache key (sort them by name)
67+
func_signature = inspect.signature(function)
68+
bad_arg_types = (inspect.Parameter.KEYWORD_ONLY, inspect.Parameter.VAR_KEYWORD)
69+
if any(arg.kind in bad_arg_types for arg in func_signature.parameters.values()):
70+
raise ValueError("Member functions with keyword only arguments can not be "
71+
"wrapped with the cached_member_function.")
72+
6173
@wraps(function)
62-
def wrapper(self, *args):
74+
def wrapper(self, *args, **kwargs):
75+
# convert all arguments to poisitonal arguments and add default arguments
76+
# for not provided arguments
77+
bound_args = func_signature.bind(self, *args, **kwargs)
78+
bound_args.apply_defaults()
79+
assert not bound_args.kwargs
80+
args = bound_args.args[1:] # remove self from args
81+
6382
try:
6483
fun_cache = self._function_cache[fname]
6584
except AttributeError:

0 commit comments

Comments
 (0)