➜

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mkurnikov
Recipients eric.smith, mkurnikov
Date 2018-09-11.21:18:48
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1536700728.42.0.0269046726804.issue34409@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2018-09-11 21:18:48mkurnikovsetrecipients: + mkurnikov, eric.smith
2018-09-11 21:18:48mkurnikovsetmessageid: <1536700728.42.0.0269046726804.issue34409@psf.upfronthosting.co.za>
2018-09-11 21:18:48mkurnikovlinkissue34409 messages
2018-09-11 21:18:48mkurnikovcreate