New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confusing message with AttributeError when attribute name matches suggestion #88821
Comments
This is related to bpo-44649. This is the simplest non-dataclasses case I could come up with. Given: class Example:
__slots__ = ('a', 'b')
def __init__(self, a):
self.a = a
obj = Example(42)
print(obj.b) I get this in 3.10 and main: Traceback (most recent call last):
File "C:\home\eric\local\python\cpython\testdc.py", line 7, in <module>
print(obj.b)
^^^^^
AttributeError: b. Did you mean: 'b'? The error message is confusing. 3.8 gives: Traceback (most recent call last):
File "testdc.py", line 7, in <module>
print(obj.b)
AttributeError: b I don't have 3.9 around to test with. Maybe don't print the "Did you mean" part if the suggestion is the same as the requested attribute? The fact that the instance variable isn't initialized is the actual error in bpo-44649, and I'll fix it there. |
Also, the dot after the first 'b' was confusing to me: I thought it had something to do with an attribute of b. And the quotes around the second 'b' were also confusing, but that's mostly because the original example initialized a class variable named 'b' with the str value 'b'. Maybe a better error would use a colon: AttributeError: foo: Did you mean: 'foo1'? Looking at 3.10s behavior, I can't decide what logic it's using to suggest something: >>> 'foo'.formatx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'formatx'. Did you mean: 'format'?
>>> class C: pass
...
>>> C().a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'C' object has no attribute 'a' |
For attribute errors we just call dir() on the object and we do a suggestion based on the names so if a name appears in the dir() then we will consider it for the suggestion. |
I think this is a good idea and will take care of many other similar cases. Thanks for the suggestion, Eric! I will prepare a PR |
It's obviously not super important, but it would be nicer if the version with the suggestion were more like the version without the suggestion. Specifically, mentioning the object type: >>> class E:
... __slots__=('a')
...
>>> E().a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: a. Did you mean: 'a'?
>>> E().b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'E' object has no attribute 'b'
>>> |
Thanks, Pablo! |
Agreed, but that is actually not related with the suggestions. We only append a string at the end of whatever exception it was there. This difference is based on the attribute error for a slot not initialized vs an attribute error for something that is not there. For example, in 3.9: --- class E:
__slots__=('blech') E().blech Traceback (most recent call last):
File "/home/pablogsal/github/cpython/lel.py", line 6, in <module>
E().blech
AttributeError: blech while class E:
__slots__=('blech') E().bluch Traceback (most recent call last):
File "/home/pablogsal/github/cpython/lel.py", line 4, in <module>
E().bluch
AttributeError: 'E' object has no attribute 'bluch' |
Got it. Thanks for jumping on this quickly. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: