Building Nested Dataclasses from JSON

hard
Unit 24 · Validation0/1 to milestone

Implement from_dict(cls, data) that builds a dataclass instance from a dict, recursively, using the field type hints:

  • a field annotated with another dataclass is built from its nested dict
  • list[X] builds each element (recursively if X is a dataclass)
  • Optional[X] (i.e. X | None) accepts None
  • fields with defaults may be missing from data; missing required fields raise TypeError
  • extra keys in data raise TypeError
  • primitive fields (int, str, float, bool) must already have the right type (bool is not accepted for int), otherwise raise TypeError mentioning the field name
Pro problem

Unlock this and every premium problem across all tracks — plus hidden-test grading, advanced AI engineering modules and new content every week.

Start 7-day free trial
solution.py · Python 3.13
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Building Nested Dataclasses from JSON · TechGradify