KEMBAR78
Wrong error message when trying to delete type `__name__` · Issue #119494 · python/cpython · GitHub
Skip to content

Wrong error message when trying to delete type __name__ #119494

@da-woods

Description

@da-woods

Bug report

Bug description:

This is a fairly minor bug - the behaviour the interpreter disallows looks correct but the error messages look unhelpful.

>>> class C:
...   pass
...
>>> C.__name__
'C'
>>> C.__name__ = 'D'
>>> C.__name__
'D'
>>> del C.__name__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot delete '__name__' attribute of immutable type 'D'

You can reassign the name fine and this works (because the type isn't immutable). However if you try to delete the name then it tells you that it's because the type is immutable.

In 3.9 this changes and it just reports:

>>> del C.__name__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't delete C.__name__

Relatedly, if you try to delete the name of an immutable type:

>>> del dict.__name__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot set '__name__' attribute of immutable type 'dict'

it talks about setting the name rather than deleting it.

This is in

check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name)

CPython versions tested on:

3.10, 3.11, 3.12, 3.13

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions