mkurnikov <maxim.kurni...@gmail.com> added the comment:
from pprint import pprint from typing import List, Any, Dict import dataclasses from dataclasses import field def service_interface_dict_factory(obj: Any) -> Dict[str, Any]: print(type(obj)) # <- type(obj) here is a list, but there's no way to understand whether it's a ServiceInterface or # InputVar except for looking for the presence of certain keys which is not very convenient return dict(obj) @dataclasses.dataclass class InputVar(object): name: str required: bool = False options: Dict[str, Any] = field(default_factory=dict) @dataclasses.dataclass class ServiceInterface(object): input: List[InputVar] = field(default_factory=list) if __name__ == '__main__': inputvar_inst = InputVar(name='myinput', required=False, options={'default': 'mytext'}) interface = ServiceInterface(input=[inputvar_inst]) outdict = dataclasses.asdict(interface, dict_factory=service_interface_dict_factory) print('outdict', end=' ') pprint(outdict) # prints: # outdict {'input': [{'name': 'myinput', # 'options': {'default': 'mytext'}, # 'required': False}]} # desirable output # {'input': [{ # 'name': 'myinput', # 'required': False, # 'default': 'mytext' # }]} # "default" key moved to the root of the dictionary (inside list) ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue34409> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com