diff options
author | T <tnie@tuta.io> | 2023-03-13 20:46:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-13 20:46:35 (GMT) |
commit | 71e37d907905b0504c5bb7b25681adeea2157492 (patch) | |
tree | 02f0f081a14cf8909aed93b5cddc9e62a7fb7cf8 /Lib/test/test_dataclasses.py | |
parent | 85ba8a3e03707092800cbf2a29d95e0b495e3cb7 (diff) | |
download | cpython-71e37d907905b0504c5bb7b25681adeea2157492.zip cpython-71e37d907905b0504c5bb7b25681adeea2157492.tar.gz cpython-71e37d907905b0504c5bb7b25681adeea2157492.tar.bz2 |
gh-98169 dataclasses.astuple support DefaultDict (#98170)
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rw-r--r-- | Lib/test/test_dataclasses.py | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 76bed0c..46d4e0f 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -1706,19 +1706,17 @@ class TestCase(unittest.TestCase): def test_helper_asdict_defaultdict(self): # Ensure asdict() does not throw exceptions when a # defaultdict is a member of a dataclass - @dataclass class C: mp: DefaultDict[str, List] - dd = defaultdict(list) dd["x"].append(12) c = C(mp=dd) d = asdict(c) - assert d == {"mp": {"x": [12]}} - assert d["mp"] is not c.mp # make sure defaultdict is copied + self.assertEqual(d, {"mp": {"x": [12]}}) + self.assertTrue(d["mp"] is not c.mp) # make sure defaultdict is copied def test_helper_astuple(self): # Basic tests for astuple(), it should return a new tuple. @@ -1847,6 +1845,21 @@ class TestCase(unittest.TestCase): t = astuple(c, tuple_factory=list) self.assertEqual(t, ['outer', T(1, ['inner', T(11, 12, 13)], 2)]) + def test_helper_astuple_defaultdict(self): + # Ensure astuple() does not throw exceptions when a + # defaultdict is a member of a dataclass + @dataclass + class C: + mp: DefaultDict[str, List] + + dd = defaultdict(list) + dd["x"].append(12) + c = C(mp=dd) + t = astuple(c) + + self.assertEqual(t, ({"x": [12]},)) + self.assertTrue(t[0] is not dd) # make sure defaultdict is copied + def test_dynamic_class_creation(self): cls_dict = {'__annotations__': {'x': int, 'y': int}, } |