Skip to main content

dict2dc - dictionary to dataclass parsing

Python PyPI version Downloads GitHub stars last release status

dict2dc is a small Python library that helps to parse Python dicts to dataclass structures. E.g., these dicts could originate from JSON deserialization.

The library supports nested dataclasses, collections and union types. It always tries to initiate the best matching class.

🚀 Getting started

Install it in your Python project:

pip install dict2dc # or uv add or poetry add...

💻 Usage Examples

Deserialization/Parsing

import datetime
import dataclasses
from collections.abc import Collection
from dict2dc.dict2dc import Dict2Dc


class MyDataSubClass1:
    a: str


@dataclasses.dataclass(frozen=True)
class MyDataSubClass2:
    a: int


@dataclasses.dataclass
class MyDataClass:
    a: int
    b: set[str]
    c: Collection[MyDataSubClass1 | MyDataSubClass2] = dataclasses.field(default_factory=list)
    d: datetime.datetime | None = None


data = {
    "a": 1,
    "b": ["Hello", "World"],  # JSON does not know sets thus they probably come as list
    "c": [{"a": "Hello"}, {"a": 42}],
    "d": "2026-01-26T10:33:48.703386",
}  # e.g., from json.load() or response.json()

Dict2Dc().from_dict_enforced(data, cls=MyDataClass)

Serialization

JSON

import dataclasses
import json
from dict2dc.dc2json import Dc2Json, DcJsonEncoder
from dict2dc.models.base import UNTOUCHED, UNTOUCHED_TYPE


@dataclasses.dataclass
class MyDataClass:
    a: str
    b: str | None = None
    c: int | UNTOUCHED_TYPE = UNTOUCHED  # Unmodified, the key will be omitted in the serializable dict


my_obj: MyDataClass = MyDataClass(a="Hello World")
serializable = Dc2Json().as_serializable(my_obj)
json.dumps(serializable)  # {"a": "Hello World", "b": None}

# Alternative
json.dumps(serializable, cls=DcJsonEncoder)

Query Parameters

from dict2dc.dc2query import Dc2Query

my_obj: MyDataClass = MyDataClass(...)
query_params = Dc2Query().as_query_params(my_obj)
requests.get("https://example.com", params=query_params)

🛠️ Advanced Usage

The parsing has some opinionated defaults, e.g., regarding datetime representation. If you want to adjust them or add your own parsing methods, you can pass a mapping from target type to method to the constructor:

import datetime
from dict2dc.dict2dc import Dict2Dc


def convert(v: str) -> datetime.datetime:
    return datetime.datetime.fromtimestamp(float(v), tz=datetime.UTC)


dict2dc = Dict2Dc(
    special_conversions={
        datetime.datetime: convert
    }
)

dict2dc.from_dict_enforced(
    {
        "created": "1769416853"
    }, cls=MyDataClass
)

The default conversions can be found in dict2dc.py. You can also use the optional replace parameter of the constructor to omit the default conversions entirely.
Note: These conversions are currently only triggered if the value to parse is a string.

To adjust the serialization helpers (Dc2Json, DcJsonEncoder, Dc2Query), you need to create your own subclasses. There you can override the existing methods or add your own for custom conversions.

Download files

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

Source Distribution

dict2dc-0.3.0.tar.gz (72.5 kB view details)

Uploaded Source

Built Distribution

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

dict2dc-0.3.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file dict2dc-0.3.0.tar.gz.

File metadata

  • Download URL: dict2dc-0.3.0.tar.gz
  • Upload date:
  • Size: 72.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.30 {"installer":{"name":"uv","version":"0.11.30","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":true}

File hashes

Hashes for dict2dc-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9be27d5460b2dff57939ca117af409c0fb3805ff9f8d7d1b53f5b193a3e2214c
MD5 190c39be65e01eee5e29b3ee1c0ec336
BLAKE2b-256 322fefe8b990c29db91771cccf0c8dbed694e5c1543755ee5f632f6e9c028e99

See more details on using hashes here.

File details

Details for the file dict2dc-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: dict2dc-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.30 {"installer":{"name":"uv","version":"0.11.30","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":true}

File hashes

Hashes for dict2dc-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b6b5154b15b51fe5a9dcf8626981986e11abe6cea4173cb2861dabf85d94a1c8
MD5 47a5ed0f154df9b21254b26647e04868
BLAKE2b-256 a9e481e1468692840657c67509834d7a97e32e2d34df4b43815353901c056407

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.0

2 files

0.1.3

2 files

0.1.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page