8000 Better error message for assignment to non-existent __slots__ · Issue #96663 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Better error message for assignment to non-existent __slots__  #96663
Closed
@Gobot1234

Description

@Gobot1234

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0