Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serialization of nested data classes

See original GitHub issue

I’m using SimpleParsing version 0.0.19post0 installed through pip. Consider the situation that you have several data classes with unrelated options. Each of these classes contains only primitive type variables and is itself easily serializable. Now, I want to have a wrapper data class that contains instances of the other classes and I want to be able to serialize (works) and deserialize it (doesn’t work). I’m sure this is supposed to work and I’m only doing something wrong, but here is an example:

from __future__ import annotations
from dataclasses import dataclass
from simple_parsing.helpers import Serializable

class Opts1(Serializable):
    a: int = 64
    b: float = 1.0

class Opts2(Serializable):
    a: int = 32
    b: float = 0.0

class Wrapper(Serializable):
    opts1: Opts1 = Opts1()
    opts2: Opts2 = Opts2()

# Show that it's not possible to deserialize nested dataclasses
opts = Wrapper()
reconstructed = Wrapper.loads_json(opts.dumps_json())

You get a warning from helpers/serialization/ that it is “Unable to find a decoding function for type IncludedOpts1. Will try to use the type as a constructor.” The deserialized Wrapper object does not contain instances of Opts1 and Opts2 but rather two dicts which shows that the deserialization failed.

I read through the example that used a Tensor and provided a custom encoding and decoding function, but I don’t see why this should be necessary here. Both classes Opts1 and Opts2 are already perfectly serializable on their own.

On a related note: While serializing and deserializing e.g. Opts1 on their own works for JSON, it throws a similar warning for YAML but it reconstructs the object correctly

opts1 = Opts1()
reconstructed1 = Opts1.loads_yaml(opts1.dumps_yaml())

Expected Behavior: Such deserialization should work out of the box. If not, I’d appreciate any tips on how to do this correctly.


  • OS: Ubuntu
  • Version 0.0.19post0
  • Python version: 3.8

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

halirutancommented, Apr 20, 2022

Uh… thanks a lot for the fast response. I was really unsure if I overlooked something in the docs. Great that it works now.

lebricecommented, Apr 20, 2022

Ok the PR closed this automatically, but I’ll make a quick simple-parsing 0.0.19.post1 release.

Thanks for pointing this out!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Json serialization of nested dataclasses - Stack Overflow
I would need to take the question about json serialization of @dataclass from Make the Python json encoder support Python's new dataclasses ......
Read more >
lidatong/dataclasses-json: Easily serialize Data ... - GitHub
Easily serialize Data Classes to and from JSON. ... It's recursive (see caveats below), so you can easily work with nested dataclasses. In...
Read more >
Dataclass Wizard — Dataclass Wizard 0.22.2 documentation
The primary use is as a fast serialization framework that enables dataclass instances to be converted to/from JSON; this works well in particular...
Read more >
Gson - Serializing Inner Classes - Tutorialspoint
In this chapter, we will explain serialization/deserialization of classes having inner classes. Nested Inner Class example. Student student = new Student(); ...
Read more >
Creating nested dataclass objects in Python - GeeksforGeeks
Here, we'll tackle the idea of having nested dataclass objects in our program. Even though dataclasses are easy to use, they still increase ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found