You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, the error message for an attribute that isn't included in a class's __slots__ is harder to understand than I think it needs to be.
Python 3.12.0a0 (heads/main:4114bcc, Sep 7 2022, 19:35:54) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> classFoo:
... __slots__= ("bar",)
...
>>> Foo().not_an_attribute =1234
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute 'not_an_attribute'
Pitch
I think the message can be improved in multiple ways and be made more similar to the error message for attribute access:
Make the error message contain a note as to why the assignment failed. Ideally, this would look something like
Make the error message more forgiving in the case of a typo.
>>> Foo().bat ="hello world"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Foo' cannot have attribute 'bat' set as it is not included in its __slots__. Did you mean: 'bar'?
Support more introspection on the raised AttributeError as currently name and obj are None.
The object has no __dict__ for setting new attributes. In this case, it can only have attributes and attribute descriptors that are found on the type. The absence of a __dict__ can come about in various ways, not just via __slots__. For example:
>>> (1).not_an_attribute =1234
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'not_an_attribute'
The generic error message in _PyObject_GenericSetAttrWithDict()1 could be something like the following: "'%.100s' object has no attribute '%U' and no __dict__ for setting new attributes".
Footnotes
_thread._local is a use case for this function's dict parameter. An instance of _thread._local has a different dict for each thread. ↩
Feature or enhancement
Currently, the error message for an attribute that isn't included in a class's
__slots__
is harder to understand than I think it needs to be.Pitch
I think the message can be improved in multiple ways and be made more similar to the error message for attribute access:
Linked PRs
The text was updated successfully, but these errors were encountered: