Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
Assignees
Labels
3.13bugs and security fixes 3.14bugs and security fixes 3.15new features, bugs and security fixes stdlibStandard Library Python modules in the Lib/ directory type-bugAn unexpected behavior, bug, or error
@johnslavik

Description

Bug report

Bug description:

Example instance of this bug in functools:

from functools import singledispatchmethod
 
class C:
 @singledispatchmethod
 def sdm(self, a):
 pass
 
C().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:

class C:
 foobar = 1
 
 def __getattr__(self, _):
 raise AttributeError
C().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

Labels

3.13bugs and security fixes 3.14bugs and security fixes 3.15new features, bugs and security fixes stdlibStandard Library Python modules in the Lib/ directory type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      AltStyle によって変換されたページ (->オリジナル) /