Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read a nested dataclass #53

Open
marco-luzzara opened this issue Nov 4, 2022 · 2 comments
Open

Cannot read a nested dataclass #53

marco-luzzara opened this issue Nov 4, 2022 · 2 comments

Comments

@marco-luzzara
Copy link

marco-luzzara commented Nov 4, 2022

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.

@dfurtado
Copy link
Owner

dfurtado commented Nov 9, 2022

Hello @marco-luzzara 👋🏼

I have though about this use case before and I found very difficult to solve, for instance:

  • How would you handle type checking for the nested dataclasses? This is essencial for this lib.
  • Type inference for the properties of all nested classes?
  • Need to implement some type of recursive solution since the dataclass may have multiple nested dataclasses.
  • With the item above what about performance?

For the case of the writer is simple since the data goes to a text file, the other way around things start to get complicated.

@marco-luzzara
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants