Skip to content

Commit

Permalink
pythongh-98169 dataclasses.astuple support DefaultDict
Browse files Browse the repository at this point in the history
  • Loading branch information
kwsp committed Oct 11, 2022
1 parent e82d977 commit 81d9170
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
24 changes: 16 additions & 8 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,15 +1325,15 @@ def _asdict_inner(obj, dict_factory):
# generator (which is not true for namedtuples, handled
# above).
return type(obj)(_asdict_inner(v, dict_factory) for v in obj)
elif isinstance(obj, dict) and hasattr(type(obj), 'default_factory'):
# obj is a defaultdict, which has a different constructor from
# dict as it requires the default_factory as its first arg.
# https://bugs.python.org/issue35540
result = type(obj)(getattr(obj, 'default_factory'))
for k, v in obj.items():
result[_asdict_inner(k, dict_factory)] = _asdict_inner(v, dict_factory)
return result
elif isinstance(obj, dict):
if hasattr(type(obj), 'default_factory'):
# obj is a defaultdict, which has a different constructor from
# dict as it requires the default_factory as its first arg.
# https://bugs.python.org/issue35540
result = type(obj)(getattr(obj, 'default_factory'))
for k, v in obj.items():
result[_asdict_inner(k, dict_factory)] = _asdict_inner(v, dict_factory)
return result
return type(obj)((_asdict_inner(k, dict_factory),
_asdict_inner(v, dict_factory))
for k, v in obj.items())
Expand Down Expand Up @@ -1386,6 +1386,14 @@ def _astuple_inner(obj, tuple_factory):
# above).
return type(obj)(_astuple_inner(v, tuple_factory) for v in obj)
elif isinstance(obj, dict):
if hasattr(type(obj), 'default_factory'):
# obj is a defaultdict, which has a different constructor from
# dict as it requires the default_factory as its first arg.
# https://bugs.python.org/issue35540
result = type(obj)(getattr(obj, 'default_factory'))
for k, v in obj.items():
result[_astuple_inner(k, tuple_factory)] = _asdict_inner(v, tuple_factory)
return result
return type(obj)((_astuple_inner(k, tuple_factory), _astuple_inner(v, tuple_factory))
for k, v in obj.items())
else:
Expand Down
21 changes: 17 additions & 4 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,19 +1680,17 @@ class C:
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.
Expand Down Expand Up @@ -1821,6 +1819,21 @@ class C:
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},
}
Expand Down

0 comments on commit 81d9170

Please sign in to comment.