Skip to content
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

Better error message for assignment to non-existent __slots__ #96663

Closed
Gobot1234 opened this issue Sep 7, 2022 · 1 comment
Closed

Better error message for assignment to non-existent __slots__ #96663

Gobot1234 opened this issue Sep 7, 2022 · 1 comment
Labels
type-feature A feature request or enhancement

Comments

@Gobot1234
Copy link
Contributor

Gobot1234 commented Sep 7, 2022

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:

  1. 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__
  2. 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'?
  3. Support more introspection on the raised AttributeError as currently name and obj are None.

Linked PRs

@Gobot1234 Gobot1234 added the type-feature A feature request or enhancement label Sep 7, 2022
@eryksun
Copy link
Contributor

eryksun commented Sep 7, 2022

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

  1. _thread._local is a use case for this function's dict parameter. An instance of _thread._local has a different dict for each thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants