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
TomlDataclassandJsonDataclassare provided as base mixins.- Root comments, field comments, renamed keys, and nested dataclasses are supported.
Enum,Literal,Optional, lists, tuples, sets, anddict[str, T]are supported.- Custom loading is configured through
dacite.Config. - Custom serialization is integrated through
json.JSONEncoderandtomlkitencoders.
Notes
- TOML comments are emitted only for TOML serialization.
- Dictionary keys are required to be strings.
- Fields with
Nonevalues 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
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 serde_dataclass-0.0.3.tar.gz.
File metadata
- Download URL: serde_dataclass-0.0.3.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77cfc8c243701d9ecf3fcc7849c3902417f48ae36f7a18dae84b53ecf27ca12d
|
|
| MD5 |
3332be209fef8ea7ca9d7ddc3b99da3b
|
|
| BLAKE2b-256 |
4cba4808a4cb83492132bcb0715d110c45b1c348836dc78b18d3bd22e4663380
|
File details
Details for the file serde_dataclass-0.0.3-py3-none-any.whl.
File metadata
- Download URL: serde_dataclass-0.0.3-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8bf00968a554e4acd39a48c72eddc6e7c27c50184258d787c7edad2ab1778215
|
|
| MD5 |
3ef0433fcbdd932755477abd72254608
|
|
| BLAKE2b-256 |
685d06f5ca3114c50a7df0f5fd59d464a1a3fd7843ec718cf4f2d18baf6439bf
|