Skip to content

Misleading error report when we implement __getattr__ and a property raises an AttributeError exception #131271

@FabriceSalvaire

Description

@FabriceSalvaire

Feature or enhancement

Proposal:

There are many reports on the web about this behaviour.

But look at this code and how Python 3.13 reacts:

class Foo:

    def __init__(self):
        self.bar = None

    def __getattr__(self, name):
        print(f'catched {name}')

    @property
    def aproperty(self):
        self.bar.foo

    def afunc(self):
        self.bar.foo

_ = Foo()

_.afunc()
# Traceback (most recent call last):
#   File "property-issue.py", line 18, in <module>
#     _.afunc()
#     ~~~~~~~^^
#   File "property-issue.py", line 14, in afunc
#     self.bar.foo
# AttributeError: 'NoneType' object has no attribute 'foo'

_.aproperty
# catched aproperty

It looks weird to me. Why Python should react differently for the same code depending on it is defined as a func or a property ?
Moreover, I would expect __getattr__ catch AttributeError only on Foo.__dict__.
And a better error reporting and a note on the documentation about this behaviour.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

See #69171 #90143
And especially #52968

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions