[issue34409] Add a way to customize iteration over fields in asdict() for the nested dataclasses

2018-09-12 Thread Eric V. Smith
Eric V. Smith added the comment: Thanks, Raymond. I agree that this request is too specialized to add to dataclasses. Any proposal here or that I've been able to think of complicate the API for the much more common use case of not needing asdict() specialization. To the original poster: I

[issue34409] Add a way to customize iteration over fields in asdict() for the nested dataclasses

2018-09-12 Thread Raymond Hettinger
Raymond Hettinger added the comment: I recommend passing on this feature request as being too specialized and beyond the scope of what data classes are intended to do. FWIW, the information needed by a user to write their own customized iteration patterns is publicly available. One of

[issue34409] Add a way to customize iteration over fields in asdict() for the nested dataclasses

2018-09-11 Thread mkurnikov
mkurnikov 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)

[issue34409] Add a way to customize iteration over fields in asdict() for the nested dataclasses

2018-09-11 Thread Eric V. Smith
Eric V. Smith added the comment: I've been thinking about this, but I don't have a suggestion on how to improve the API. Maybe some sort of visitor pattern? I'm open to concrete ideas. -- ___ Python tracker

[issue34409] Add a way to customize iteration over fields in asdict() for the nested dataclasses

2018-08-14 Thread mkurnikov
mkurnikov 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

[issue34409] Add a way to customize iteration over fields in asdict() for the nested dataclasses

2018-08-14 Thread Eric V. Smith
Change by Eric V. Smith : -- assignee: -> eric.smith ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

[issue34409] Add a way to customize iteration over fields in asdict() for the nested dataclasses

2018-08-14 Thread Eric V. Smith
Eric V. Smith added the comment: Could you show some example dataclass instances? Also, show the output you get with asdict(), and show what output you'd like to get instead. I'm not sure I understand it correctly from the description you've given. Thanks! -- nosy: +eric.smith

[issue34409] Add a way to customize iteration over fields in asdict() for the nested dataclasses

2018-08-14 Thread mkurnikov
New submission from mkurnikov : Suppose I have two dataclasses: @dataclass class NestedDataclass(object): name: str options: Dict[str, Any] = field(default_factory=dict) @dataclass class RootDataclass(object): nested_list: List[NestedDataclass] I want a dict under the key