Skip to content

Commit

Permalink
If there's an AttributeError within a @lazyproperty, expose it
Browse files Browse the repository at this point in the history
Since this is a caching decorator, there's no use-case for
intentionally returning an AttributeError.

This changes makes coding errors within these methods much easier
to debug.
  • Loading branch information
brondsem committed Sep 11, 2024
1 parent 59689c8 commit 1769eb6
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions ming/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

class EmptyClass: pass

# inspired by https://github.com/davidhalter/jedi/blob/master/jedi/inference/utils.py
class UncaughtAttributeError(Exception):
pass

class LazyProperty:

def __init__(self, func):
Expand All @@ -12,8 +16,13 @@ def __init__(self, func):
self.__doc__ = func.__doc__

def __get__(self, obj, klass=None):
if obj is None: return None
result = obj.__dict__[self.__name__] = self._func(obj)
if obj is None:
return None
try:
result = self._func(obj)
except AttributeError as e:
raise UncaughtAttributeError(str(e)) from e
obj.__dict__[self.__name__] = result
return result

class ContextualProxy:
Expand Down

0 comments on commit 1769eb6

Please sign in to comment.