Skip to content

Bare AttributeError in __getattr__ shows a garbled message with attribute suggestions #143811

Description

@johnslavik

Bug report

Bug description:

Example instance of this bug in functools:

fromfunctoolsimportsingledispatchmethodclassC:
@singledispatchmethoddefsdm(self, a):
passC().sdm.__code__

Traceback gotten (-) vs. expected (+):

 Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
C().sdm.__code__
File "/home/bswck/Python/cpython/Lib/functools.py", line 1096, in __getattr__
raise AttributeError
-AttributeError: . Did you mean: '__call__'?+AttributeError: '_singledispatchmethod_get' object has no attribute '__code__'. Did you mean: '__call__'?

The right fix is not to change functools, but to change traceback.TracebackException to handle bare AttributeErrors (complete the message). It already knows what the attribute is, otherwise it wouldn't compute suggestions.

Minimal reproducer:

classC:
foobar=1def__getattr__(self, _):
raiseAttributeErrorC().foboar
Traceback (most recent call last):
File "/home/bswck/Python/cpython/t.py", line 7, in <module>
C().foboar
File "/home/bswck/Python/cpython/t.py", line 5, in __getattr__
raise AttributeError
AttributeError: . Did you mean: 'foobar'?

Backportable to 3.13 and 3.14.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

Labels

3.13bugs and security fixes3.14bugs and security fixes3.15pre-release feature fixes, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions