refactor: proper TypeError handling in memoize decorator (#16074)

Co-authored-by: Victor Sadkov <victor.sadkov@cloudkitchens.com>
This commit is contained in:
Victor
2021-08-06 19:03:52 -07:00
committed by GitHub
parent 85329c374e
commit 85ae8e3477

View File

@@ -41,16 +41,18 @@ class _memoized:
if self.is_method:
key.append(tuple([getattr(args[0], v, None) for v in self.watch]))
key = tuple(key) # type: ignore
if key in self.cache:
return self.cache[key]
try:
value = self.func(*args, **kwargs)
if key in self.cache:
return self.cache[key]
except TypeError as ex:
# Uncachable -- for instance, passing a list as an argument.
raise TypeError("Function cannot be memoized") from ex
value = self.func(*args, **kwargs)
try:
self.cache[key] = value
return value
except TypeError:
# uncachable -- for instance, passing a list as an argument.
# Better to not cache than to blow up entirely.
return self.func(*args, **kwargs)
except TypeError as ex:
raise TypeError("Function cannot be memoized") from ex
return value
def __repr__(self) -> str:
"""Return the function's docstring."""