Skip to main content

Convert dataclasses to TOML, preserving comments.

Project description

serde-dataclass

TOML and JSON serialization for Python dataclasses is provided through a small, explicit API.

Comment-preserving TOML output, renamed keys, nested dataclasses, and typed round-trips are supported.

Documentation

Project documentation is provided through MkDocs.

pip install -e .[docs]
mkdocs serve

Detailed usage is documented in docs/.

Installation

pip install serde-dataclass

For development and example dependencies:

pip install -e .[dev]

Quick Start

from dataclasses import dataclass, field
from enum import Enum
from typing import Literal

from serde_dataclass import TomlDataclass


class Mode(str, Enum):
    DEV = "dev"
    PROD = "prod"


@dataclass
class Database:
    host: str = field(metadata={"description": "Database host"})
    port: int = field(default=5432, metadata={"description": "Database port"})


@dataclass
class AppConfig(TomlDataclass):
    """Application configuration"""

    app_name: str = field(
        default="demo",
        metadata={"description": "Application name", "toml": "app-name"},
    )
    log_level: Literal["debug", "info", "warning", "error"] = field(
        default="info",
        metadata={"description": "Logging verbosity", "toml": "log-level"},
    )
    mode: Mode = field(default=Mode.DEV, metadata={"description": "Runtime mode"})
    database: Database = field(
        default_factory=lambda: Database(host="localhost"),
        metadata={"description": "Database settings"},
    )


cfg = AppConfig()
text = cfg.to_toml()
loaded = AppConfig.from_toml(text)

assert loaded == cfg
print(text)

Example output:

# Application configuration

app-name = "demo" # Application name
log-level = "info" # Logging verbosity
mode = "dev" # Runtime mode

# Database settings
[database]
host = "localhost" # Database host
port = 5432 # Database port

Summary

  • TomlDataclass and JsonDataclass are provided as base mixins.
  • Root comments, field comments, renamed keys, and nested dataclasses are supported.
  • Enum, Literal, Optional, lists, tuples, sets, and dict[str, T] are supported.
  • Custom loading is configured through dacite.Config.
  • Custom serialization is integrated through json.JSONEncoder and tomlkit encoders.

Notes

  • TOML comments are emitted only for TOML serialization.
  • Dictionary keys are required to be strings.
  • Fields with None values are omitted from TOML output.

Development

pytest -q

License

MIT

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

serde_dataclass-0.0.2.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

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

serde_dataclass-0.0.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file serde_dataclass-0.0.2.tar.gz.

File metadata

  • Download URL: serde_dataclass-0.0.2.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for serde_dataclass-0.0.2.tar.gz
Algorithm Hash digest
SHA256 85d82d44044f3896216d85aadc21b5a27f0a0beea83fe2f4d5aeb7c3b4f2287f
MD5 d3f1bcb60fa756eaad4e3d32d5697489
BLAKE2b-256 a0a10df115390c17fef2b4f63cf2001d2bfa3384331440b195d28e4bd0a4957d

See more details on using hashes here.

File details

Details for the file serde_dataclass-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for serde_dataclass-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 20f87032a6f438a47ff7b09705d985f522311801867e9e56520d7444af3c86a8
MD5 5eba0ac12eb2a3cd94200aa259a9913c
BLAKE2b-256 c6c447f63ce7a8b1d5f5f22ecd472fb62b61c3a6383f5721f5f35c0b9b928694

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