diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 39b835b03fc599..14f94285d3f3c2 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4078,7 +4078,7 @@ class E(D): self.assertEqual(C2.__subclasses__(), [D]) with self.assertRaisesRegex(TypeError, - "cannot delete '__bases__' attribute of immutable type"): + "cannot delete '__bases__' attribute of type 'D'"): del D.__bases__ with self.assertRaisesRegex(TypeError, 'can only assign non-empty tuple'): D.__bases__ = () @@ -5062,7 +5062,7 @@ class X: with self.assertRaises(TypeError) as cm: type(X).__dict__["__doc__"].__delete__(X) - self.assertIn("cannot delete '__doc__' attribute of immutable type 'X'", str(cm.exception)) + self.assertIn("cannot delete '__doc__' attribute of type 'X'", str(cm.exception)) self.assertEqual(X.__doc__, "banana") def test_qualname(self): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst new file mode 100644 index 00000000000000..b8076fedda3c4f --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst @@ -0,0 +1 @@ +Exception text when trying to delete attributes of types was clarified. diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 0a222a5384f67b..7d03655e77a0bb 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1465,15 +1465,15 @@ static PyMemberDef type_members[] = { static int check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) { - if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) { + if (!value) { PyErr_Format(PyExc_TypeError, - "cannot set '%s' attribute of immutable type '%s'", + "cannot delete '%s' attribute of type '%s'", name, type->tp_name); return 0; } - if (!value) { + if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) { PyErr_Format(PyExc_TypeError, - "cannot delete '%s' attribute of immutable type '%s'", + "cannot set '%s' attribute of immutable type '%s'", name, type->tp_name); return 0; }