You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
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.
The text was updated successfully, but these errors were encountered:
Hi @dfurtado ,
Thanks for the answer 👍 Honestly, I don't see how these problems can be worse than a runtime error. Right now, dataclasses with nested dataclasses cannot be used at all. I think this case could be handled without affecting the performance and functionalities of the rest of the library. By the way, if you found this scenario particularly difficult to integrate into your library, it is reasonable and I will not insist.
Another approach could be flattening the root dataclass, for example:
@dataclass
class NestedClass:
nested_prop: int
@dataclass
class Dataclass1:
nested_class: NestedClass
root_prop: str
The resulting csv header would become: root_prop,nested_class.nested_prop
But it would complicate both the writing and reading process. It was just another idea.
Thanks again
Nested dataclasses are not handled by the
DataclassReader
. While theDataclassWriter
serializes a nested dataclass as a tuple (thanks to the recursive behavior ofdataclasses.astuple()
), theDataclassReader
tries to initialize the nested dataclass with:dataclass-csv/dataclass_csv/dataclass_reader.py
Line 232 in 2dc71be
This cannot work because
value
is a string representing a tuple, whereasfield_type
(that in this case is a dataclass) constructor expects the fields necessary to build the dataclass.The solution is pretty simple:
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.The text was updated successfully, but these errors were encountered: