Skip to content

Commit 2939cba

Browse files
committed
Test frozendict subclasses
1 parent 9666d7e commit 2939cba

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

Lib/test/test_dict.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,12 @@ class FrozenDict(frozendict):
17311731
pass
17321732

17331733

1734+
class FrozenDictSlots(frozendict):
1735+
__slots__ = ('attr',)
1736+
def __init__(self, *args, **kwargs):
1737+
self.attr = 123
1738+
1739+
17341740
class FrozenDictTests(unittest.TestCase):
17351741
def test_copy(self):
17361742
d = frozendict(x=1, y=2)
@@ -1773,10 +1779,8 @@ def test_repr(self):
17731779
d = frozendict(x=1, y=2)
17741780
self.assertEqual(repr(d), "frozendict({'x': 1, 'y': 2})")
17751781

1776-
class MyFrozenDict(frozendict):
1777-
pass
1778-
d = MyFrozenDict(x=1, y=2)
1779-
self.assertEqual(repr(d), "MyFrozenDict({'x': 1, 'y': 2})")
1782+
d = FrozenDict(x=1, y=2)
1783+
self.assertEqual(repr(d), "FrozenDict({'x': 1, 'y': 2})")
17801784

17811785
def test_hash(self):
17821786
# hash() doesn't rely on the items order
@@ -1826,13 +1830,23 @@ def __new__(self):
18261830
self.assertEqual(created, frozendict(x=1))
18271831

18281832
def test_pickle(self):
1829-
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
1833+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
18301834
for fd in (
18311835
frozendict(),
18321836
frozendict(x=1, y=2),
1837+
FrozenDict(x=1, y=2),
1838+
FrozenDictSlots(x=1, y=2),
18331839
):
1834-
p = pickle.dumps(fd, proto)
1835-
self.assertEqual(fd, pickle.loads(p))
1840+
with self.subTest(fd=fd, proto=proto):
1841+
if proto >= 2:
1842+
p = pickle.dumps(fd, proto)
1843+
fd2 = pickle.loads(p)
1844+
self.assertEqual(fd2, fd)
1845+
self.assertEqual(type(fd2), type(fd))
1846+
else:
1847+
# protocol 0 and 1 don't support frozendict
1848+
with self.assertRaises(TypeError):
1849+
pickle.dumps(fd, proto)
18361850

18371851

18381852
if __name__ == "__main__":

0 commit comments

Comments
 (0)