mirror of
https://github.com/python/cpython.git
synced 2025-09-20 15:40:32 +00:00
gh-102578: Optimise setting and deleting mutable attributes on non-dataclass subclasses of frozen dataclasses (gh-102573)
This commit is contained in:
parent
90f1d77717
commit
ee6f8413a9
3 changed files with 52 additions and 6 deletions
|
@ -2767,6 +2767,19 @@ class TestFrozen(unittest.TestCase):
|
|||
c.i = 5
|
||||
self.assertEqual(c.i, 10)
|
||||
|
||||
def test_frozen_empty(self):
|
||||
@dataclass(frozen=True)
|
||||
class C:
|
||||
pass
|
||||
|
||||
c = C()
|
||||
self.assertFalse(hasattr(c, 'i'))
|
||||
with self.assertRaises(FrozenInstanceError):
|
||||
c.i = 5
|
||||
self.assertFalse(hasattr(c, 'i'))
|
||||
with self.assertRaises(FrozenInstanceError):
|
||||
del c.i
|
||||
|
||||
def test_inherit(self):
|
||||
@dataclass(frozen=True)
|
||||
class C:
|
||||
|
@ -2890,6 +2903,37 @@ class TestFrozen(unittest.TestCase):
|
|||
self.assertEqual(s.y, 10)
|
||||
self.assertEqual(s.cached, True)
|
||||
|
||||
with self.assertRaises(FrozenInstanceError):
|
||||
del s.x
|
||||
self.assertEqual(s.x, 3)
|
||||
with self.assertRaises(FrozenInstanceError):
|
||||
del s.y
|
||||
self.assertEqual(s.y, 10)
|
||||
del s.cached
|
||||
self.assertFalse(hasattr(s, 'cached'))
|
||||
with self.assertRaises(AttributeError) as cm:
|
||||
del s.cached
|
||||
self.assertNotIsInstance(cm.exception, FrozenInstanceError)
|
||||
|
||||
def test_non_frozen_normal_derived_from_empty_frozen(self):
|
||||
@dataclass(frozen=True)
|
||||
class D:
|
||||
pass
|
||||
|
||||
class S(D):
|
||||
pass
|
||||
|
||||
s = S()
|
||||
self.assertFalse(hasattr(s, 'x'))
|
||||
s.x = 5
|
||||
self.assertEqual(s.x, 5)
|
||||
|
||||
del s.x
|
||||
self.assertFalse(hasattr(s, 'x'))
|
||||
with self.assertRaises(AttributeError) as cm:
|
||||
del s.x
|
||||
self.assertNotIsInstance(cm.exception, FrozenInstanceError)
|
||||
|
||||
def test_overwriting_frozen(self):
|
||||
# frozen uses __setattr__ and __delattr__.
|
||||
with self.assertRaisesRegex(TypeError,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue