Skip to content

Commit

Permalink
refactor: proper TypeError handling in memoize decorator (#16074)
Browse files Browse the repository at this point in the history
Co-authored-by: Victor Sadkov <victor.sadkov@cloudkitchens.com>
  • Loading branch information
sabiroid and sabiroid committed Aug 7, 2021
1 parent 85329c3 commit 85ae8e3
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions superset/utils/memoized.py
Expand Up @@ -41,16 +41,18 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any:
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."""
Expand Down

0 comments on commit 85ae8e3

Please sign in to comment.