da-woods · GitHub

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

Read the original on github.com ↗