Skip to content

Cannot read a nested dataclassΒ #53

@marco-luzzara

Description

@marco-luzzara

Nested dataclasses are not handled by the DataclassReader. While the DataclassWriter serializes a nested dataclass as a tuple (thanks to the recursive behavior of dataclasses.astuple()), the DataclassReader tries to initialize the nested dataclass with:

transformed_value = field_type(value)

This cannot work because value is a string representing a tuple, whereas field_type (that in this case is a dataclass) constructor expects the fields necessary to build the dataclass.

The solution is pretty simple:

if dataclasses.is_dataclass(field_type):
    import ast
    fields = ast.literal_eval(value)
    values[field.name] = field_type(*fields)
    continue

This IF-statement could be placed in dataclass_reader.py, line 230. This is just a draft, but if you think it would be a good improvement I can propose a PR with error handling and tests as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions