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.
This project is in its early stages, but it is has minimum viable functionality for my use-cases.
Up next would be:
- Supporting unions of Dataclasses (incl. discriminated unions)
- Improving docs.
- JSON import/export in
IOMixin. - Bug hunting and edge cases around the dataclass spec and typing system.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hill_dataclassio-0.1.0.tar.gz.
File metadata
- Download URL: hill_dataclassio-0.1.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b32cd173790efb43a00345b958929a38e852e3e28a238e8d01d049b2dbd7b2f7
|
|
| MD5 |
6f8d3aa20752f4cbd54e1c86e49ffe92
|
|
| BLAKE2b-256 |
5c7e8c934f96428d5345abd52410f198b0c51be8d939cb91e5c244b239f64378
|
File details
Details for the file hill_dataclassio-0.1.0-py3-none-any.whl.
File metadata
- Download URL: hill_dataclassio-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47093ba7a26ca7f8889045de5e82eb230db5b4b05b57ccb1e0068f17393d351a
|
|
| MD5 |
01dd3cb3883417a5fd04a482aba6b4e3
|
|
| BLAKE2b-256 |
58a7a0a327e1ad68495be6ebc1a9e6c0a29a26f15ed7f6028cded4c8942e447d
|