-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
gh-96663: Add a better error message for __dict__-less classes setattr #103232
Conversation
Objects/object.c
Outdated
"'%.100s' object has no attribute '%U' and no " | ||
"__dict__ for setting new attributes", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't find the tests for this bit of code, are there any?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an example that raises the error message when a __dict__
is available, so there has to be a check in the second path.
I'm not entirely sure why the tests are failing >>> class B:
... y = 0
... __slots__ = ('z', 'foo')
...
>>> B().fod = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'B' object has no attribute 'fod' and no __dict__ for setting new attributes. Did you mean: 'foo'? AFAICT this should be the error that you get? Unless it's failing due to not being printed. |
@Gobot1234 The suggestion ( |
Should this perhaps check for class Foo:
__slots__ = ("x", "y")
def __setattr__(self, name, val):
if name == "all":
self.x = val
self.y = val
else:
super().__setattr__(name, val)
f = Foo()
f.all = 5 |
@pablogsal since this is an error message change would you be willing to review? |
Start testing on 3.13, and fix a test for the change introduced in python/cpython#103232.
I think this addresses all the issues I have with the current message. Thanks to Eryk for the pointer as to where I should be editing.