diff options
author | Massimiliano Culpo <massimiliano.culpo@gmail.com> | 2019-03-30 01:11:44 +0100 |
---|---|---|
committer | Peter Scheibel <scheibel1@llnl.gov> | 2019-03-29 17:11:44 -0700 |
commit | e3f00750e85d33282d6a710b44eba1c5efe44de2 (patch) | |
tree | 5c8a1ae5ff6b6210bd58d34fb5d5bedd98ee1109 | |
parent | def5b237633f6cfa51d5619d34880a993c5d3a32 (diff) | |
download | spack-e3f00750e85d33282d6a710b44eba1c5efe44de2.tar.gz spack-e3f00750e85d33282d6a710b44eba1c5efe44de2.tar.bz2 spack-e3f00750e85d33282d6a710b44eba1c5efe44de2.tar.xz spack-e3f00750e85d33282d6a710b44eba1c5efe44de2.zip |
Update llnl.util.lang.memoized so that Sphinx can extract signature (#11055)
Replace the original implementation of the "memoized" decorator with
an implementation that exposes the docstring and arguments of the
wrapped function. This is achieved using functools.wraps.
-rw-r--r-- | lib/spack/llnl/util/lang.py | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/lib/spack/llnl/util/lang.py b/lib/spack/llnl/util/lang.py index 70c646bed0..11820a7f58 100644 --- a/lib/spack/llnl/util/lang.py +++ b/lib/spack/llnl/util/lang.py @@ -168,30 +168,24 @@ def union_dicts(*dicts): return result -class memoized(object): - """Decorator that caches the results of a function, storing them - in an attribute of that function.""" - - def __init__(self, func): - self.func = func - self.cache = {} +def memoized(func): + """Decorator that caches the results of a function, storing them in + an attribute of that function. + """ + func.cache = {} - def __call__(self, *args): + @functools.wraps(func) + def _memoized_function(*args): if not isinstance(args, collections.Hashable): # Not hashable, so just call the function. - return self.func(*args) + return func(*args) - if args not in self.cache: - self.cache[args] = self.func(*args) - return self.cache[args] + if args not in func.cache: + func.cache[args] = func(*args) - def __get__(self, obj, objtype): - """Support instance methods.""" - return functools.partial(self.__call__, obj) + return func.cache[args] - def clear(self): - """Expunge cache so that self.func will be called again.""" - self.cache.clear() + return _memoized_function def list_modules(directory, **kwargs): |