mirror of
https://github.com/python/cpython.git
synced 2025-08-24 02:35:59 +00:00
bpo-35540 dataclasses.asdict now supports defaultdict fields (gh-32056)
This commit is contained in:
parent
a4b7794887
commit
c46a423a52
3 changed files with 28 additions and 2 deletions
|
@ -1325,6 +1325,14 @@ 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):
|
||||
return type(obj)((_asdict_inner(k, dict_factory),
|
||||
_asdict_inner(v, dict_factory))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue