Skip to content

bpo-35540: Add collections.defaultdict support to dataclasses.{asdict,astuple} #11361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import keyword
import builtins
import functools
import collections
import _thread


Expand Down Expand Up @@ -1077,6 +1078,12 @@ 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, collections.defaultdict):
# defaultdict does not have the same constructor than dict and must be
# handled separately
return type(obj)(obj.default_factory, ((_asdict_inner(k, dict_factory),
_asdict_inner(v, dict_factory))
for k, v in obj.items()))
elif isinstance(obj, dict):
return type(obj)((_asdict_inner(k, dict_factory),
_asdict_inner(v, dict_factory))
Expand Down Expand Up @@ -1129,6 +1136,12 @@ def _astuple_inner(obj, tuple_factory):
# generator (which is not true for namedtuples, handled
# above).
return type(obj)(_astuple_inner(v, tuple_factory) for v in obj)
elif isinstance(obj, collections.defaultdict):
# defaultdict does not have the same constructor than dict and must be
# handled separately
return type(obj)(obj.default_factory, ((_asdict_inner(k, dict_factory),
_asdict_inner(v, dict_factory))
for k, v in obj.items()))
elif isinstance(obj, dict):
return type(obj)((_astuple_inner(k, tuple_factory), _astuple_inner(v, tuple_factory))
for k, v in obj.items())
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,17 @@ class C:
self.assertIsNot(d['f'], t)
self.assertEqual(d['f'].my_a(), 6)

def test_helper_asdict_defaultdict(self):
@dataclass
class C:
d: dict

from collections import defaultdict
c = C(defaultdict(int))
self.assertEqual(asdict(c), {
'd': defaultdict(int)
})

def test_helper_astuple(self):
# Basic tests for astuple(), it should return a new tuple.
@dataclass
Expand Down Expand Up @@ -1620,6 +1631,15 @@ 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):
@dataclass
class C:
d: dict

from collections import defaultdict
c = C(defaultdict(int))
self.assertEqual(astuple(c), (defaultdict(int),))

def test_dynamic_class_creation(self):
cls_dict = {'__annotations__': {'x': int, 'y': int},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`dataclasses.asdict` and `dataclasses.astuple` now accept instances with
`collections.defaultdict` attributes. Patch by Rémi Lapeyre.