Closed
Description
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.
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.
>>> class Foo:
... __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
AttributeError: 'Foo' cannot have attribute 'not_an_attribute' set as it is not included in its __slots__
- 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.