[Python-ideas] AtributeError inside __get__

Nick Timkovich prometheus235 at gmail.com
Sun Dec 25 16:03:23 EST 2016


Are you saying that hasattr returning False was hiding a bug or is a bug?
The former could be annoying to track down, though hasattr(X, 'y') == True.
For the latter, having hasattr return False if an AttributeError is raised
would allow the property decorator to retain identical functionality if it
is used to replace a (sometimes) existing attribute.

On Sun, Dec 25, 2016 at 1:24 PM, Zahari Dim <zaharid at gmail.com> wrote:

> 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.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161225/4f40d314/attachment.html>


More information about the Python-ideas mailing list