[Python-ideas] The AttributeError/__getattr__ mechanism

Chris Angelico rosuav at gmail.com
Fri Nov 27 16:23:08 EST 2015


On Sat, Nov 28, 2015 at 7:32 AM, 王珺 <wjun77 at gmail.com> wrote:
> At first I think of adding AttributeMissError. In that case,
> object.__getattr__ (here self.x.__getattr__) could turn AttributeMissError
> to AttributeError.
> But after some consideration I think there's no need to add an extra
> Exception. Just ignore the title of that issue.

So... if I'm understanding the issue correctly, it's about the
interaction of @property and __getattr__? You're talking about this as
a boundary over which AttributeError changes. Borrowing your example
from the tracker issue:

class property(property):
    def __get__(self, *a):
        try:
            return super().__get__(*a)
        except AttributeError as e:
            raise RuntimeError("Property raised AttributeError") from e

class A():
    def __init__(self, x=None):
        self.x = x

    @property
    def t(self):
        return self.x.t

    def __getattr__(self, name):
        return 'default'

print(A().t)


Traceback (most recent call last):
  File "attrerr.py", line 4, in __get__
    return super().__get__(*a)
  File "attrerr.py", line 14, in t
    return self.x.t
AttributeError: 'NoneType' object has no attribute 't'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "attrerr.py", line 19, in <module>
    print(A().t)
  File "attrerr.py", line 6, in __get__
    raise RuntimeError("Property raised AttributeError") from e
RuntimeError: Property raised AttributeError


This would create a boundary, same as PEP 479 does for StopIteration,
across which AttributeError becomes RuntimeError.

This could be incorporated into the built-in property if desired, or
kept on a per-module basis with the above.

ChrisA


More information about the Python-ideas mailing list