mkurnikov <[email protected]> added the comment:
Cleanest thing I could think of is:
1. Extract dataclass_to_dict function from _asdict_inner as:
def dataclass_asdict(obj, dict_factory):
result = []
for f in fields(obj):
value = _asdict_inner(getattr(obj, f.name), dict_factory)
result.append((f.name, value))
return dict_factory(result)
2. Add "asdict" parameter to the dataclass decorator (with default value of
dataclass_to_dict function)
@dataclass(asdict=specific_dcls_dict_factory)
class MyDataclass:
pass
3. Change check to
def _asdict_inner(obj, dict_factory):
if _is_dataclass_instance(obj):
return getattr(obj, _PARAMS).asdict(obj)
# ... other code
Other solution could be to add parameter "directly_serializable"(smth like
that), add check for this parameter
def _asdict_inner(obj, dict_factory):
if _is_dataclass_instance(obj):
if getattr(obj, _PARAMS).directly_serializable:
return dict_factory(obj)
# ... other code
and force user to process everything in dict_factory function.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue34409>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com