Skip to content

Commit 4aed319

Browse files
authored
gh-119775: Remove ability to create immutable types with mutable bases (#119776)
1 parent fd6cd62 commit 4aed319

File tree

4 files changed

+13
-35
lines changed

4 files changed

+13
-35
lines changed

Doc/whatsnew/3.14.rst

+2
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,5 @@ Deprecated
258258
Removed
259259
-------
260260

261+
* Creating :c:data:`immutable types <Py_TPFLAGS_IMMUTABLETYPE>` with mutable
262+
bases was deprecated since 3.12 and now raises a :exc:`TypeError`.

Lib/test/test_capi/test_misc.py

+3-25
Original file line numberDiff line numberDiff line change
@@ -777,33 +777,11 @@ def test_pytype_fromspec_with_repeated_slots(self):
777777
with self.assertRaises(SystemError):
778778
_testcapi.create_type_from_repeated_slots(variant)
779779

780-
@warnings_helper.ignore_warnings(category=DeprecationWarning)
781780
def test_immutable_type_with_mutable_base(self):
782-
# Add deprecation warning here so it's removed in 3.14
783-
warnings._deprecated(
784-
'creating immutable classes with mutable bases', remove=(3, 14))
785-
786-
class MutableBase:
787-
def meth(self):
788-
return 'original'
789-
790-
with self.assertWarns(DeprecationWarning):
791-
ImmutableSubclass = _testcapi.make_immutable_type_with_base(
792-
MutableBase)
793-
instance = ImmutableSubclass()
781+
class MutableBase: ...
794782

795-
self.assertEqual(instance.meth(), 'original')
796-
797-
# Cannot override the static type's method
798-
with self.assertRaisesRegex(
799-
TypeError,
800-
"cannot set 'meth' attribute of immutable type"):
801-
ImmutableSubclass.meth = lambda self: 'overridden'
802-
self.assertEqual(instance.meth(), 'original')
803-
804-
# Can change the method on the mutable base
805-
MutableBase.meth = lambda self: 'changed'
806-
self.assertEqual(instance.meth(), 'changed')
783+
with self.assertRaisesRegex(TypeError, 'Creating immutable type'):
784+
_testcapi.make_immutable_type_with_base(MutableBase)
807785

808786
def test_pynumber_tobase(self):
809787
from _testcapi import pynumber_tobase
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Creating :c:data:`immutable types <Py_TPFLAGS_IMMUTABLETYPE>` with mutable
2+
bases was deprecated since 3.12 and now raises a :exc:`TypeError`.

Objects/typeobject.c

+6-10
Original file line numberDiff line numberDiff line change
@@ -4613,16 +4613,12 @@ _PyType_FromMetaclass_impl(
46134613
goto finally;
46144614
}
46154615
if (!_PyType_HasFeature(b, Py_TPFLAGS_IMMUTABLETYPE)) {
4616-
if (PyErr_WarnFormat(
4617-
PyExc_DeprecationWarning,
4618-
0,
4619-
"Creating immutable type %s from mutable base %s is "
4620-
"deprecated, and slated to be disallowed in Python 3.14.",
4621-
spec->name,
4622-
b->tp_name))
4623-
{
4624-
goto finally;
4625-
}
4616+
PyErr_Format(
4617+
PyExc_TypeError,
4618+
"Creating immutable type %s from mutable base %N",
4619+
spec->name, b
4620+
);
4621+
goto finally;
46264622
}
46274623
}
46284624
}

0 commit comments

Comments
 (0)