Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-103000: Optimise dataclasses asdict/astuple for common types #103005

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

Merged
merged 15 commits into from
Apr 10, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Revert function call skip for _asdict_inner
  • Loading branch information
DavidCEllis committed Mar 24, 2023
commit 0209f3cfd25889bc6593daa90e9c4c4bcf412ac0
30 changes: 10 additions & 20 deletions 30 Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,12 +1315,12 @@ class C:


def _asdict_inner(obj, dict_factory):
if _is_dataclass_instance(obj):
if type(obj) in _ATOMIC_TYPES:
return obj
elif _is_dataclass_instance(obj):
result = []
for f in fields(obj):
value = getattr(obj, f.name)
if type(value) not in _ATOMIC_TYPES:
value = _asdict_inner(value, dict_factory)
value = _asdict_inner(getattr(obj, f.name), dict_factory)
result.append((f.name, value))
return dict_factory(result)
elif isinstance(obj, tuple) and hasattr(obj, '_fields'):
Expand All @@ -1343,33 +1343,23 @@ def _asdict_inner(obj, dict_factory):
# namedtuples, we could no longer call asdict() on a data
# structure where a namedtuple was used as a dict key.

return type(obj)(*[
v if type(v) in _ATOMIC_TYPES else _asdict_inner(v, dict_factory)
for v in obj
])
return type(obj)(*[_asdict_inner(v, dict_factory) for v in obj])
elif isinstance(obj, (list, tuple)):
# Assume we can create an object of this type by passing in a
# generator (which is not true for namedtuples, handled
# above).
return type(obj)(
v if type(v) in _ATOMIC_TYPES else _asdict_inner(v, dict_factory)
for v in obj
)
return type(obj)(_asdict_inner(v, dict_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.
result = type(obj)(getattr(obj, 'default_factory'))
for k, v in obj.items():
k = k if type(k) in _ATOMIC_TYPES else _asdict_inner(k, dict_factory)
v = v if type(v) in _ATOMIC_TYPES else _asdict_inner(v, dict_factory)
result[k] = v
result[_asdict_inner(k, dict_factory)] = _asdict_inner(v, dict_factory)
return result

return type(obj)(
(k if type(k) in _ATOMIC_TYPES else _asdict_inner(k, dict_factory),
v if type(v) in _ATOMIC_TYPES else _asdict_inner(v, dict_factory))
for k, v in obj.items())
return type(obj)((_asdict_inner(k, dict_factory),
_asdict_inner(v, dict_factory))
for k, v in obj.items())
else:
return copy.deepcopy(obj)

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.