diff options
author | Petr Viktorin <encukou@gmail.com> | 2022-08-04 14:13:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-04 14:13:45 (GMT) |
commit | a613fedd6e18e4ab382cf81ec767e1135fc949a7 (patch) | |
tree | 55b93ee54133040ea0a267d614e901fc669910a6 /Lib/test/test_capi.py | |
parent | 000c3874bfa63507293c22249881f6cfc61af311 (diff) | |
download | cpython-a613fedd6e18e4ab382cf81ec767e1135fc949a7.zip cpython-a613fedd6e18e4ab382cf81ec767e1135fc949a7.tar.gz cpython-a613fedd6e18e4ab382cf81ec767e1135fc949a7.tar.bz2 |
gh-95388: Deprecate creating immutable types with mutable bases (GH-95533)
Diffstat (limited to 'Lib/test/test_capi.py')
-rw-r--r-- | Lib/test/test_capi.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py index 013229a..c743557 100644 --- a/Lib/test/test_capi.py +++ b/Lib/test/test_capi.py @@ -15,6 +15,7 @@ import textwrap import threading import time import unittest +import warnings import weakref from test import support from test.support import MISSING_C_DOCSTRINGS @@ -644,6 +645,34 @@ class CAPITest(unittest.TestCase): with self.assertRaises(SystemError): _testcapi.create_type_from_repeated_slots(variant) + 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() + + 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') + + def test_pynumber_tobase(self): from _testcapi import pynumber_tobase self.assertEqual(pynumber_tobase(123, 2), '0b1111011') |