From 1769eb639fa570914e6316e434a67432f803afc3 Mon Sep 17 00:00:00 2001 From: Dave Brondsema Date: Tue, 10 Sep 2024 17:06:50 -0400 Subject: [PATCH] If there's an AttributeError within a @LazyProperty, expose it 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. --- ming/utils.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ming/utils.py b/ming/utils.py index c36a553..acdf0ff 100644 --- a/ming/utils.py +++ b/ming/utils.py @@ -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): @@ -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: