[issue45985] AttributeError from @property inadvertantly flows into __getattr__
Ofer Koren
report at bugs.python.org
Sun Dec 5 06:02:26 EST 2021
New submission from Ofer Koren <koreno at gmail.com>:
There's For quite a long time I've been seeing this bug:
>>> class A():
... @property
... def foo(self):
... return self.bar # <---- this is where it all starts ('bar' isn't found)
...
... def __getattr__(self, attr):
... raise AttributeError(attr) # <--- let's pretend our getattr couldn't find the attr
>>> A().foo
Traceback (most recent call last):
File "t.py", line 13, in <module>
A().foo
File "t.py", line 10, in __getattr__
raise AttributeError(attr)
AttributeError: foo
So an AttributeError spawned by `self.bar` caused us to "fallback" on __getattr__ with attr='foo', leading the naive code there to produce a very confusing error message.
My workaround was to use a @safe_property decorator, one that catches AttributeError exceptions and converts them to RuntimeErrors instead, so that they don't flow into the unsuspecting __getattr__.
I believe python should adopt this behavior into the built-in property decorator, perhaps with a more appropriate exception type.
----------
messages: 407701
nosy: koreno
priority: normal
severity: normal
status: open
title: AttributeError from @property inadvertantly flows into __getattr__
type: behavior
versions: Python 3.10, Python 3.11, Python 3.6, Python 3.7, Python 3.8, Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45985>
_______________________________________
More information about the Python-bugs-list
mailing list