Skip to content

Commit

Permalink
simlify dict_factory case
Browse files Browse the repository at this point in the history
  • Loading branch information
tybug committed Dec 15, 2023
1 parent e9ba14c commit cc2db94
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions hypothesis-python/src/hypothesis/internal/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,10 @@ def dataclass_asdict(obj, *, dict_factory=dict):

def _asdict_inner(obj, dict_factory):
if dataclasses._is_dataclass_instance(obj):
if dict_factory is dict:
return {
f.name: _asdict_inner(getattr(obj, f.name), dict)
for f in dataclasses.fields(obj)
}
else: # pragma: no cover
result = []
for f in dataclasses.fields(obj):
value = _asdict_inner(getattr(obj, f.name), dict_factory)
result.append((f.name, value))
return dict_factory(result)
return dict_factory(
(f.name, _asdict_inner(getattr(obj, f.name), dict_factory))
for f in dataclasses.fields(obj)
)
elif isinstance(obj, tuple) and hasattr(obj, "_fields"):
return type(obj)(*[_asdict_inner(v, dict_factory) for v in obj])
elif isinstance(obj, (list, tuple)):
Expand Down

0 comments on commit cc2db94

Please sign in to comment.