Skip to main content

Deep dataclasses with nested structures and validation

Project description

deep_dataclasses

Coverage Python License

Define nested dataclass hierarchies as clean, readable schemas — no boilerplate.


The Problem

Python's @dataclass requires you to define each level of a nested hierarchy separately, then wire them together manually with field(default_factory=...):

@dataclass
class GrandChild:
    grandchild_str: str = "grandchild1"
    grandchild_num: int = 1

@dataclass
class Child:
    grandchild: GrandChild = field(default_factory=GrandChild)
    child_str: str = "child"

@dataclass
class Parent:
    child: Child = field(default_factory=Child)
    parent_str: str = "parent"

This is verbose, hard to read at a glance, and the nesting structure is only implicit.


The Solution

@deep_dataclass lets you express the same hierarchy as a natural nested schema:

from deep_dataclasses import deep_dataclass

@deep_dataclass(autosnake=True)
class DeepParent:
    class Child:
        class Grandchild:
            grandchild_str: str = "grandchild1"
            grandchild_num: int = 1
        child_str: str = "child"
    parent_str: str = "parent"

print(DeepParent().child.grandchild)

The decorator recursively converts nested class blocks into proper @dataclass types, wiring up field(default_factory=...) automatically.


Fully Compatible with dataclasses

@deep_dataclass produces standard dataclass instances — all stdlib tools work as expected:

from dataclasses import asdict

d1 = Parent()   # vanilla dataclass hierarchy
d2 = DeepParent()  # deep_dataclass equivalent

# Structural equality across different class definitions
asdict(d1) == asdict(d2)          # True

# Round-trip via dict
Parent(**asdict(d1)) == d1        # True
DeepParent(**asdict(d2)) == d2    # True

Installation

pip install deep-dataclasses

Comparison

Feature @dataclass @deep_dataclass
Nested hierarchy Manual, verbose Inline, readable
field(default_factory=...) Required per field Automatic
asdict() / == / __repr__
frozen, slots, etc. ✅ (tested)
Type validation ❌ (by design)

Relationship to PEP 712

Python 3.13's PEP 712 added field(converter=...) for per-field coercion. @deep_dataclass complements this by handling the structural boilerplate one level up — the class hierarchy itself.


Status

Early release. Core functionality is complete and covered at 100%. API may evolve — feedback welcome on discuss.python.org.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deep_dataclasses-0.1.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

deep_dataclasses-0.1.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file deep_dataclasses-0.1.0.tar.gz.

File metadata

  • Download URL: deep_dataclasses-0.1.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for deep_dataclasses-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8f51802cb123f86048c2372043f8279a0a087e7a93ebd5cb4ceef5cb6ae773c0
MD5 e230bfdd454f5f387b5a8a51422d8a18
BLAKE2b-256 a2a417f48af0c1bdaece4c6b098074a22c6f711f516aa8c2dd907bc5b3860554

See more details on using hashes here.

File details

Details for the file deep_dataclasses-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for deep_dataclasses-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 347be6ea6cbfb158383c33588fdd5ec88d83e7550e501352c4f1f772931361d0
MD5 2ff106b56c1c0422ba1f638e2e19e655
BLAKE2b-256 9bd6cc11d4d72488535cf4dcf92a76003e3fd4203c25d8efbea5484821b4707c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page