AttrLike dict: better exceptions

This commit is contained in:
Klaas van Schelven
2024-11-20 16:30:48 +01:00
parent ca7a062c7a
commit 0ad878d1bc
2 changed files with 21 additions and 4 deletions

View File

@@ -70,9 +70,15 @@ class AttrLikeDict(dict):
return item in self
def __getattr__(self, item):
if item not in self:
raise AttributeError(item)
return self[item]
# attribute errors are to be understood at the call site; as they are for regular object's missing attributes.
__tracebackhide__ = True
try:
return self[item]
except KeyError:
# "from None": this is so directly caused by the KeyError that cause and effect are the same and cause must
# be hidden.
raise AttributeError(item) from None
_settings = None

View File

@@ -36,8 +36,19 @@ DEFAULTS = {
class AttrLikeDict(dict):
def __hasattr__(self, item):
return item in self
def __getattr__(self, item):
return self[item]
# attribute errors are to be understood at the call site; as they are for regular object's missing attributes.
__tracebackhide__ = True
try:
return self[item]
except KeyError:
# "from None": this is so directly caused by the KeyError that cause and effect are the same and cause must
# be hidden.
raise AttributeError(item) from None
_settings = None