with self.assertRaises(SystemError):
_testcapi.create_type_from_repeated_slots(variant)
- @warnings_helper.ignore_warnings(category=DeprecationWarning)
def test_immutable_type_with_mutable_base(self):
- # Add deprecation warning here so it's removed in 3.14
- warnings._deprecated(
- 'creating immutable classes with mutable bases', remove=(3, 14))
-
- class MutableBase:
- def meth(self):
- return 'original'
-
- with self.assertWarns(DeprecationWarning):
- ImmutableSubclass = _testcapi.make_immutable_type_with_base(
- MutableBase)
- instance = ImmutableSubclass()
+ class MutableBase: ...
- self.assertEqual(instance.meth(), 'original')
-
- # Cannot override the static type's method
- with self.assertRaisesRegex(
- TypeError,
- "cannot set 'meth' attribute of immutable type"):
- ImmutableSubclass.meth = lambda self: 'overridden'
- self.assertEqual(instance.meth(), 'original')
-
- # Can change the method on the mutable base
- MutableBase.meth = lambda self: 'changed'
- self.assertEqual(instance.meth(), 'changed')
+ with self.assertRaisesRegex(TypeError, 'Creating immutable type'):
+ _testcapi.make_immutable_type_with_base(MutableBase)
def test_pynumber_tobase(self):
from _testcapi import pynumber_tobase
goto finally;
}
if (!_PyType_HasFeature(b, Py_TPFLAGS_IMMUTABLETYPE)) {
- if (PyErr_WarnFormat(
- PyExc_DeprecationWarning,
- 0,
- "Creating immutable type %s from mutable base %s is "
- "deprecated, and slated to be disallowed in Python 3.14.",
- spec->name,
- b->tp_name))
- {
- goto finally;
- }
+ PyErr_Format(
+ PyExc_TypeError,
+ "Creating immutable type %s from mutable base %N",
+ spec->name, b
+ );
+ goto finally;
}
}
}