[Python-ideas] AtributeError inside __get__

Zahari Dim zaharid at gmail.com
Sun Dec 25 13:24:44 EST 2016


Hi,

The other day I came across a particularly ugly bug. A simplified case
goes like:

class X:
    @property
    def y(self):
        return self.nonexisting

hasattr(X(),'y')

This returns False because hasattr calls the property which in turn
raises an AttributeError which is used to determine that the property
doesn't exist, even if it does. This is arguably unexpected and
surprising and can be very difficult to understand if it happens
within a large codebase. Given the precedent with generator_stop,
which solves a similar problem for StopIteration, I was wondering if
it would be possible to have the __get__ method convert the
AttributeErrors raised inside it to RuntimeErrors.

The situation with this is a little more complicated because there
could be a (possibly strange) where one might want to raise an
AttributeError inside __get__. But maybe the specification can be
changed so either `raise ForceAttributeError()` or `return
NotImplemented` achieves the same effect.


Merry Christmas!
Zahari.


More information about the Python-ideas mailing list