gh-119494: Fix error messages for deleting/setting type attributes (#119495)
Some checks are pending
Tests / Windows MSI (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Ubuntu SSL tests with AWS-LC (push) Blocked by required conditions
Tests / Android (aarch64) (push) Blocked by required conditions
Tests / Android (x86_64) (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run

This commit is contained in:
da-woods 2025-09-18 22:08:49 +01:00 committed by GitHub
parent 3eec897752
commit ac5c5d42a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 6 deletions

View file

@ -4078,7 +4078,7 @@ class ClassPropertiesAndMethods(unittest.TestCase):
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 ClassPropertiesAndMethods(unittest.TestCase):
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):

View file

@ -0,0 +1 @@
Exception text when trying to delete attributes of types was clarified.

View file

@ -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;
}