mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-98169 dataclasses.astuple support DefaultDict (#98170)
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
This commit is contained in:
parent
85ba8a3e03
commit
71e37d9079
3 changed files with 35 additions and 13 deletions
|
@ -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},
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue