Skip to content

Commit d3ed33a

Browse files
Merge some tests with existing ones
1 parent ef4c814 commit d3ed33a

File tree

1 file changed

+60
-63
lines changed

1 file changed

+60
-63
lines changed

Lib/test/test_dataclasses/__init__.py

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,43 +3052,38 @@ class C(base):
30523052

30533053

30543054
class TestFrozen(unittest.TestCase):
3055-
def test_frozen(self):
3056-
@dataclass(frozen=True)
3057-
class C:
3058-
i: int
3055+
# Some tests have a subtest with a slotted dataclass.
3056+
# See https://github.com/python/cpython/issues/105936 for the reasons.
30593057

3060-
c = C(10)
3061-
self.assertEqual(c.i, 10)
3062-
with self.assertRaises(FrozenInstanceError):
3063-
c.i = 5
3064-
self.assertEqual(c.i, 10)
3058+
def test_frozen(self):
3059+
for slots in (False, True):
3060+
with self.subTest(slots=slots):
30653061

3066-
def test_frozen_empty(self):
3067-
@dataclass(frozen=True)
3068-
class C:
3069-
pass
3062+
@dataclass(frozen=True, slots=slots)
3063+
class C:
3064+
i: int
30703065

3071-
c = C()
3072-
self.assertNotHasAttr(c, 'i')
3073-
with self.assertRaises(FrozenInstanceError):
3074-
c.i = 5
3075-
self.assertNotHasAttr(c, 'i')
3076-
with self.assertRaises(FrozenInstanceError):
3077-
del c.i
3066+
c = C(10)
3067+
self.assertEqual(c.i, 10)
3068+
with self.assertRaises(FrozenInstanceError):
3069+
c.i = 5
3070+
self.assertEqual(c.i, 10)
30783071

3079-
def test_frozen_slotted(self):
3080-
# See https://github.com/python/cpython/pull/144021
3081-
@dataclass(frozen=True, slots=True)
3082-
class C:
3083-
pass
3072+
def test_frozen_empty(self):
3073+
for slots in (False, True):
3074+
with self.subTest(slots=slots):
30843075

3085-
c = C()
3086-
# Mutating not defined fields must raise FrozenInstanceError.
3087-
with self.assertRaises(FrozenInstanceError):
3088-
c.any_field = 5
3076+
@dataclass(frozen=True, slots=slots)
3077+
class C:
3078+
pass
30893079

3090-
with self.assertRaises(FrozenInstanceError):
3091-
del c.any_field
3080+
c = C()
3081+
self.assertNotHasAttr(c, 'i')
3082+
with self.assertRaises(FrozenInstanceError):
3083+
c.i = 5
3084+
self.assertNotHasAttr(c, 'i')
3085+
with self.assertRaises(FrozenInstanceError):
3086+
del c.i
30923087

30933088
def test_inherit(self):
30943089
@dataclass(frozen=True)
@@ -3284,41 +3279,43 @@ class D(I):
32843279
d.i = 5
32853280

32863281
def test_non_frozen_normal_derived(self):
3287-
# See bpo-32953.
3282+
# See bpo-32953 and https://github.com/python/cpython/issues/105936
3283+
for slots in (False, True):
3284+
with self.subTest(slots=slots):
32883285

3289-
@dataclass(frozen=True)
3290-
class D:
3291-
x: int
3292-
y: int = 10
3286+
@dataclass(frozen=True, slots=slots)
3287+
class D:
3288+
x: int
3289+
y: int = 10
32933290

3294-
class S(D):
3295-
pass
3296-
3297-
s = S(3)
3298-
self.assertEqual(s.x, 3)
3299-
self.assertEqual(s.y, 10)
3300-
s.cached = True
3301-
3302-
# But can't change the frozen attributes.
3303-
with self.assertRaises(FrozenInstanceError):
3304-
s.x = 5
3305-
with self.assertRaises(FrozenInstanceError):
3306-
s.y = 5
3307-
self.assertEqual(s.x, 3)
3308-
self.assertEqual(s.y, 10)
3309-
self.assertEqual(s.cached, True)
3291+
class S(D):
3292+
pass
33103293

3311-
with self.assertRaises(FrozenInstanceError):
3312-
del s.x
3313-
self.assertEqual(s.x, 3)
3314-
with self.assertRaises(FrozenInstanceError):
3315-
del s.y
3316-
self.assertEqual(s.y, 10)
3317-
del s.cached
3318-
self.assertNotHasAttr(s, 'cached')
3319-
with self.assertRaises(AttributeError) as cm:
3320-
del s.cached
3321-
self.assertNotIsInstance(cm.exception, FrozenInstanceError)
3294+
s = S(3)
3295+
self.assertEqual(s.x, 3)
3296+
self.assertEqual(s.y, 10)
3297+
s.cached = True
3298+
3299+
# But can't change the frozen attributes.
3300+
with self.assertRaises(FrozenInstanceError):
3301+
s.x = 5
3302+
with self.assertRaises(FrozenInstanceError):
3303+
s.y = 5
3304+
self.assertEqual(s.x, 3)
3305+
self.assertEqual(s.y, 10)
3306+
self.assertEqual(s.cached, True)
3307+
3308+
with self.assertRaises(FrozenInstanceError):
3309+
del s.x
3310+
self.assertEqual(s.x, 3)
3311+
with self.assertRaises(FrozenInstanceError):
3312+
del s.y
3313+
self.assertEqual(s.y, 10)
3314+
del s.cached
3315+
self.assertNotHasAttr(s, 'cached')
3316+
with self.assertRaises(AttributeError) as cm:
3317+
del s.cached
3318+
self.assertNotIsInstance(cm.exception, FrozenInstanceError)
33223319

33233320
def test_non_frozen_normal_derived_from_empty_frozen(self):
33243321
@dataclass(frozen=True)

0 commit comments

Comments
 (0)