Skip to main content

A lightweight package for converting dataclass hierarchies to and from dictionaries.

Project description

DataclassIO

DataclassIO is a simple package to enable nested dictionary conversion of dataclass hierarchies with explicit support for handling extra fields round trip, and exluding default-valued fields on export.

This project is in its early stages, but it is has minimum viable functionality for my use-cases.

Supported Types and Constructs

Currently, the following types are supported:

  • Optional expressions: Optional[T], T | None, or Union[T, None]
  • Embedded dataclasses: DataclassType (exports as dict)
  • Lists: list[T]
  • Dicts: dict[TK, TV]
  • Enums: Enum (exports as str)
  • Datetimes: datetime (imports/exports to ISO-8601 string)
  • fundamental types: e.g., int, float, str, bool

Where the T, TK, and TV type variables may be any other type listed in the table. For instance, dict[str, list[DataclassType | None]] is supported.

The library also supports common dataclass features:

  • Default values and default factories: field(default=..., default_factory=...)
  • Init-only and init=False fields
    • Note that init=False fields are still exported as usual.
  • kw_only fields and dataclasses.
  • Class variables (no impact).

Example

from dataclasses import dataclass, field

from dataclassio import IOMixin, EFS

@dataclass
class Address(IOMixin):
    city: str
    zip_code: tp.Optional[str] = None


@dataclass
class User(IOMixin):
    id: int
    name: str
    is_admin: bool = False
    address: tp.Optional[Address] = None  # <-- Address need not derive from IOMixin!
    named_addresses: dict[str, Address] = field(default_factory=dict)

## Dictionary -> Dataclass

data = {
    "id": 1,
    "name": "Alice",
    "named_addresses": {"home": {"city": "Anytown"}, "work": {"city": "Coolsville"}},
}
actual = User.from_dict(data)
assert isinstance(actual.named_addresses, dict)
assert isinstance(actual.named_addresses["home"], Address)
assert actual.named_addresses["work"].city == "Coolsville"

## Dataclass -> Dictionary
dikt = actual.to_dict(skip_defaults=True)
assert dikt == data

## Extra Fields are captured:
address_data = {
    "city": "Anytown",
    "bonus": "extra",
}
address_obj = Address.from_dict(address_data, extra_field_strategy=EFS.CAPTURE)
assert address_obj.extra_fields == {"bonus": "extra"}
assert address_obj.to_dict() == address_data  # extra fields survive round trip.

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

hill_dataclassio-0.2.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

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

hill_dataclassio-0.2.0-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file hill_dataclassio-0.2.0.tar.gz.

File metadata

  • Download URL: hill_dataclassio-0.2.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for hill_dataclassio-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6b3d1832e2a80c3b1fc48434b8f3cefb46415277899d8f0db60b9b1cabbfcc28
MD5 15f095394929aeb73fc7deea0a8d246f
BLAKE2b-256 87d3198565198d5cf589de427b034d0e442512377b99e2c527e103cc710c77ca

See more details on using hashes here.

File details

Details for the file hill_dataclassio-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: hill_dataclassio-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for hill_dataclassio-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3aed48399408305a2061e59fcf13cad615acf2fd563966223fcb0605ecb48257
MD5 6164a38ebac459ba68057aee448db01d
BLAKE2b-256 c42c5bb496043c1744192b67ad929874396c9c84673824580d836d6a7a11f97f

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