gh-96663: Add a better error message for __dict__-less classes setattr#103232
gh-96663: Add a better error message for __dict__-less classes setattr#103232gvanrossum merged 11 commits intopython:mainfrom
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.
I can't find the tests for this bit of code, are there any?
thatbirdguythatuknownot
left a comment
There was a problem hiding this comment.
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.