Skip to content
Advertisement

Nested python dataclasses with list annotations

python ^3.7. Trying to create nested dataclasses to work with complex json response. I managed to do that with creating dataclass for every level of json and using __post_init_ to set fields as objects of other dataclasses. However that creates a lot of boilerplate code and also, there is no annotation for nested objects.

This answer helped me getting closer to the solution using wrapper:

https://stackoverflow.com/a/51565863/8325015

However it does not solve it for cases where attribute is list of objects. some_attribute: List[SomeClass]

Here is example that resembles my data:

JavaScript

As you will notice. nested_deco works perfectly for attributes of MergedInstance and for attribute data of Instance but it does not load IterationResults class on iteration_results of Result.

Is there a way to achieve it?

I attach also example with my post_init solution which creates objects of classes but there is no annotation of attributes:

JavaScript

Advertisement

Answer

This doesn’t really answer your question about the nested decorators, but my initial suggestion would be to avoid a lot of hard work for yourself by making use of libraries that have tackled this same problem before.

There are lot of well known ones like pydantic which also provides data validation and is something I might recommend. If you are interested in keeping your existing dataclass structure and not wanting to inherit from anything, you can use libraries such as dataclass-wizard and dataclasses-json. The latter one offers a decorator approach which you might interest you. But ideally, the goal is to find a (efficient) JSON serialization library which already offers exactly what you need.

Here is an example using the dataclass-wizard library with minimal changes needed (no need to inherit from a mixin class). Note that I had to modify your input JSON object slightly, as it didn’t exactly match the dataclass schema otherwise. But otherwise, it looks like it should work as expected. I’ve also removed copy.deepcopy, as that’s a bit slower and we don’t need it (the helper functions won’t directly modify the dict objects anyway, which is simple enough to test)

JavaScript

Using the dataclasses-json library:

JavaScript

Bonus: Let’s say you are calling an API that returns you a complex JSON response, such as the one above. If you want to convert this JSON response to a dataclass schema, normally you’ll have to write it out by hand, which can be a bit tiresome if the structure of the JSON is especially complex.

Wouldn’t it be cool if there was a way to simplify the generation of a nested dataclass structure? The dataclass-wizard library comes with a CLI tool that accepts an arbitrary JSON input, so it should certainly be doable to auto-generate a dataclass schema given such an input.

Assume you have these contents in a testing.json file:

JavaScript

Then we run the following command:

JavaScript

And the contents of our new testing.py file:

JavaScript

That appears to more or less match the same nested dataclass structure from the original question, and best of all we didn’t need to write any of the code ourselves!

However, there’s a minor problem – because of some duplicate JSON keys, we end up with three data classes named Data. So I’ve went ahead and renamed them to Data1, Data2, and Data3 for uniqueness. And then we can do a quick test to confirm that we’re able to load the same JSON data into our new dataclass schema:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement