serde-dataclass 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- serde_dataclass-0.0.1/PKG-INFO +134 -0
- serde_dataclass-0.0.1/README.md +115 -0
- serde_dataclass-0.0.1/pyproject.toml +37 -0
- serde_dataclass-0.0.1/setup.cfg +4 -0
- serde_dataclass-0.0.1/src/serde_dataclass/__init__.py +71 -0
- serde_dataclass-0.0.1/src/serde_dataclass/core.py +435 -0
- serde_dataclass-0.0.1/src/serde_dataclass/iface.py +280 -0
- serde_dataclass-0.0.1/src/serde_dataclass.egg-info/PKG-INFO +134 -0
- serde_dataclass-0.0.1/src/serde_dataclass.egg-info/SOURCES.txt +11 -0
- serde_dataclass-0.0.1/src/serde_dataclass.egg-info/dependency_links.txt +1 -0
- serde_dataclass-0.0.1/src/serde_dataclass.egg-info/requires.txt +11 -0
- serde_dataclass-0.0.1/src/serde_dataclass.egg-info/top_level.txt +1 -0
- serde_dataclass-0.0.1/tests/test_lib.py +408 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: serde-dataclass
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Convert dataclasses to TOML, preserving comments.
|
|
5
|
+
Author: Sunip K. Mukherjee <sunipkmukherjee@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: serde,dataclass,toml,json
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: dacite>=1.8
|
|
11
|
+
Requires-Dist: tomlkit>=0.12
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
14
|
+
Requires-Dist: numpy>=1.22; extra == "dev"
|
|
15
|
+
Requires-Dist: astropy>=5.0; extra == "dev"
|
|
16
|
+
Provides-Extra: docs
|
|
17
|
+
Requires-Dist: mkdocs>=1.6; extra == "docs"
|
|
18
|
+
Requires-Dist: mkdocs-material>=9.5; extra == "docs"
|
|
19
|
+
|
|
20
|
+
# serde-dataclass
|
|
21
|
+
|
|
22
|
+
TOML and JSON serialization for Python dataclasses is provided through a small, explicit API.
|
|
23
|
+
|
|
24
|
+
Comment-preserving TOML output, renamed keys, nested dataclasses, and typed round-trips are supported.
|
|
25
|
+
|
|
26
|
+
## Documentation
|
|
27
|
+
|
|
28
|
+
Project documentation is provided through MkDocs.
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install -e .[docs]
|
|
32
|
+
mkdocs serve
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Detailed usage is documented in `docs/`.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install serde-dataclass
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
For development and example dependencies:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install -e .[dev]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Quick Start
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from dataclasses import dataclass, field
|
|
53
|
+
from enum import Enum
|
|
54
|
+
from typing import Literal
|
|
55
|
+
|
|
56
|
+
from serde_dataclass import TomlDataclass
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class Mode(str, Enum):
|
|
60
|
+
DEV = "dev"
|
|
61
|
+
PROD = "prod"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass
|
|
65
|
+
class Database:
|
|
66
|
+
host: str = field(metadata={"description": "Database host"})
|
|
67
|
+
port: int = field(default=5432, metadata={"description": "Database port"})
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass
|
|
71
|
+
class AppConfig(TomlDataclass):
|
|
72
|
+
"""Application configuration"""
|
|
73
|
+
|
|
74
|
+
app_name: str = field(
|
|
75
|
+
default="demo",
|
|
76
|
+
metadata={"description": "Application name", "toml": "app-name"},
|
|
77
|
+
)
|
|
78
|
+
log_level: Literal["debug", "info", "warning", "error"] = field(
|
|
79
|
+
default="info",
|
|
80
|
+
metadata={"description": "Logging verbosity", "toml": "log-level"},
|
|
81
|
+
)
|
|
82
|
+
mode: Mode = field(default=Mode.DEV, metadata={"description": "Runtime mode"})
|
|
83
|
+
database: Database = field(
|
|
84
|
+
default_factory=lambda: Database(host="localhost"),
|
|
85
|
+
metadata={"description": "Database settings"},
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
cfg = AppConfig()
|
|
90
|
+
text = cfg.to_toml()
|
|
91
|
+
loaded = AppConfig.from_toml(text)
|
|
92
|
+
|
|
93
|
+
assert loaded == cfg
|
|
94
|
+
print(text)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Example output:
|
|
98
|
+
|
|
99
|
+
```toml
|
|
100
|
+
# Application configuration
|
|
101
|
+
|
|
102
|
+
app-name = "demo" # Application name
|
|
103
|
+
log-level = "info" # Logging verbosity
|
|
104
|
+
mode = "dev" # Runtime mode
|
|
105
|
+
|
|
106
|
+
# Database settings
|
|
107
|
+
[database]
|
|
108
|
+
host = "localhost" # Database host
|
|
109
|
+
port = 5432 # Database port
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Summary
|
|
113
|
+
|
|
114
|
+
- `TomlDataclass` and `JsonDataclass` are provided as base [mixins](https://en.wikipedia.org/wiki/Mixin).
|
|
115
|
+
- Root comments, field comments, renamed keys, and nested dataclasses are supported.
|
|
116
|
+
- `Enum`, `Literal`, `Optional`, lists, tuples, sets, and `dict[str, T]` are supported.
|
|
117
|
+
- Custom loading is configured through `dacite.Config`.
|
|
118
|
+
- Custom serialization is integrated through `json.JSONEncoder` and `tomlkit` encoders.
|
|
119
|
+
|
|
120
|
+
## Notes
|
|
121
|
+
|
|
122
|
+
- TOML comments are emitted only for TOML serialization.
|
|
123
|
+
- Dictionary keys are required to be strings.
|
|
124
|
+
- Fields with `None` values are omitted from TOML output.
|
|
125
|
+
|
|
126
|
+
## Development
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
pytest -q
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# serde-dataclass
|
|
2
|
+
|
|
3
|
+
TOML and JSON serialization for Python dataclasses is provided through a small, explicit API.
|
|
4
|
+
|
|
5
|
+
Comment-preserving TOML output, renamed keys, nested dataclasses, and typed round-trips are supported.
|
|
6
|
+
|
|
7
|
+
## Documentation
|
|
8
|
+
|
|
9
|
+
Project documentation is provided through MkDocs.
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install -e .[docs]
|
|
13
|
+
mkdocs serve
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Detailed usage is documented in `docs/`.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install serde-dataclass
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
For development and example dependencies:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install -e .[dev]
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from dataclasses import dataclass, field
|
|
34
|
+
from enum import Enum
|
|
35
|
+
from typing import Literal
|
|
36
|
+
|
|
37
|
+
from serde_dataclass import TomlDataclass
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class Mode(str, Enum):
|
|
41
|
+
DEV = "dev"
|
|
42
|
+
PROD = "prod"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass
|
|
46
|
+
class Database:
|
|
47
|
+
host: str = field(metadata={"description": "Database host"})
|
|
48
|
+
port: int = field(default=5432, metadata={"description": "Database port"})
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass
|
|
52
|
+
class AppConfig(TomlDataclass):
|
|
53
|
+
"""Application configuration"""
|
|
54
|
+
|
|
55
|
+
app_name: str = field(
|
|
56
|
+
default="demo",
|
|
57
|
+
metadata={"description": "Application name", "toml": "app-name"},
|
|
58
|
+
)
|
|
59
|
+
log_level: Literal["debug", "info", "warning", "error"] = field(
|
|
60
|
+
default="info",
|
|
61
|
+
metadata={"description": "Logging verbosity", "toml": "log-level"},
|
|
62
|
+
)
|
|
63
|
+
mode: Mode = field(default=Mode.DEV, metadata={"description": "Runtime mode"})
|
|
64
|
+
database: Database = field(
|
|
65
|
+
default_factory=lambda: Database(host="localhost"),
|
|
66
|
+
metadata={"description": "Database settings"},
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
cfg = AppConfig()
|
|
71
|
+
text = cfg.to_toml()
|
|
72
|
+
loaded = AppConfig.from_toml(text)
|
|
73
|
+
|
|
74
|
+
assert loaded == cfg
|
|
75
|
+
print(text)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Example output:
|
|
79
|
+
|
|
80
|
+
```toml
|
|
81
|
+
# Application configuration
|
|
82
|
+
|
|
83
|
+
app-name = "demo" # Application name
|
|
84
|
+
log-level = "info" # Logging verbosity
|
|
85
|
+
mode = "dev" # Runtime mode
|
|
86
|
+
|
|
87
|
+
# Database settings
|
|
88
|
+
[database]
|
|
89
|
+
host = "localhost" # Database host
|
|
90
|
+
port = 5432 # Database port
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Summary
|
|
94
|
+
|
|
95
|
+
- `TomlDataclass` and `JsonDataclass` are provided as base [mixins](https://en.wikipedia.org/wiki/Mixin).
|
|
96
|
+
- Root comments, field comments, renamed keys, and nested dataclasses are supported.
|
|
97
|
+
- `Enum`, `Literal`, `Optional`, lists, tuples, sets, and `dict[str, T]` are supported.
|
|
98
|
+
- Custom loading is configured through `dacite.Config`.
|
|
99
|
+
- Custom serialization is integrated through `json.JSONEncoder` and `tomlkit` encoders.
|
|
100
|
+
|
|
101
|
+
## Notes
|
|
102
|
+
|
|
103
|
+
- TOML comments are emitted only for TOML serialization.
|
|
104
|
+
- Dictionary keys are required to be strings.
|
|
105
|
+
- Fields with `None` values are omitted from TOML output.
|
|
106
|
+
|
|
107
|
+
## Development
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
pytest -q
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=69", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "serde-dataclass"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "Convert dataclasses to TOML, preserving comments."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"dacite>=1.8",
|
|
13
|
+
"tomlkit>=0.12",
|
|
14
|
+
]
|
|
15
|
+
authors = [{ name = "Sunip K. Mukherjee <sunipkmukherjee@gmail.com>" }]
|
|
16
|
+
license = { text = "MIT" }
|
|
17
|
+
keywords = ["serde", "dataclass", "toml", "json"]
|
|
18
|
+
|
|
19
|
+
[project.optional-dependencies]
|
|
20
|
+
dev = [
|
|
21
|
+
"pytest>=8.0",
|
|
22
|
+
"numpy>=1.22",
|
|
23
|
+
"astropy>=5.0"
|
|
24
|
+
]
|
|
25
|
+
docs = [
|
|
26
|
+
"mkdocs>=1.6",
|
|
27
|
+
"mkdocs-material>=9.5",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[tool.pytest.ini_options]
|
|
31
|
+
testpaths = ["tests"]
|
|
32
|
+
|
|
33
|
+
[tool.setuptools]
|
|
34
|
+
package-dir = { "" = "src" }
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.packages.find]
|
|
37
|
+
where = ["src"]
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Public package interface for serde-dataclass.
|
|
2
|
+
|
|
3
|
+
The package exposes two dataclass mixins:
|
|
4
|
+
|
|
5
|
+
- ``TomlDataclass`` for TOML serialization and deserialization
|
|
6
|
+
- ``JsonDataclass`` for JSON serialization and deserialization
|
|
7
|
+
|
|
8
|
+
TOML support includes:
|
|
9
|
+
|
|
10
|
+
- root document comments from a class docstring or ``toml_config(...)``
|
|
11
|
+
- field comments via dataclass field metadata
|
|
12
|
+
- field renaming for serialized keys
|
|
13
|
+
- nested dataclasses and arrays of tables
|
|
14
|
+
- validation for ``Enum`` and ``Literal`` annotations
|
|
15
|
+
- custom loaders through ``dacite.Config``
|
|
16
|
+
|
|
17
|
+
The expected usage pattern is to inherit from one or both mixins and still
|
|
18
|
+
decorate the class with ``@dataclass``.
|
|
19
|
+
|
|
20
|
+
Example:
|
|
21
|
+
|
|
22
|
+
>>> from dataclasses import dataclass, field
|
|
23
|
+
>>> from enum import Enum
|
|
24
|
+
>>> from typing import Literal
|
|
25
|
+
>>>
|
|
26
|
+
>>> class Mode(str, Enum):
|
|
27
|
+
... DEV = "dev"
|
|
28
|
+
... PROD = "prod"
|
|
29
|
+
...
|
|
30
|
+
>>> @dataclass
|
|
31
|
+
... class Database:
|
|
32
|
+
... host: str = field(metadata={"description": "Database host"})
|
|
33
|
+
... port: int = field(default=5432, metadata={"description": "Database port"})
|
|
34
|
+
...
|
|
35
|
+
>>> @dataclass
|
|
36
|
+
... class AppConfig(TomlDataclass):
|
|
37
|
+
... '''Application configuration'''
|
|
38
|
+
... app_name: str = field(
|
|
39
|
+
... default="demo",
|
|
40
|
+
... metadata={"description": "Application name", "toml": "app-name"},
|
|
41
|
+
... )
|
|
42
|
+
... log_level: Literal["debug", "info", "warning", "error"] = field(
|
|
43
|
+
... default="info",
|
|
44
|
+
... metadata={"description": "Logging verbosity", "toml": "log-level"},
|
|
45
|
+
... )
|
|
46
|
+
... mode: Mode = field(default=Mode.DEV, metadata={"description": "Runtime mode"})
|
|
47
|
+
... database: Database = field(
|
|
48
|
+
... default_factory=lambda: Database(host="localhost"),
|
|
49
|
+
... metadata={"description": "Database settings"},
|
|
50
|
+
... )
|
|
51
|
+
...
|
|
52
|
+
>>> cfg = AppConfig()
|
|
53
|
+
>>> loaded = AppConfig.from_toml(cfg.to_toml())
|
|
54
|
+
>>> loaded == cfg
|
|
55
|
+
True
|
|
56
|
+
|
|
57
|
+
For fuller usage guidance, see the repository documentation under ``docs/``.
|
|
58
|
+
"""
|
|
59
|
+
from importlib.metadata import version
|
|
60
|
+
|
|
61
|
+
from .iface import JsonDataclass, TomlDataclass, json_config, toml_config
|
|
62
|
+
from .core import TypeChecker
|
|
63
|
+
|
|
64
|
+
__version__ = version(__package__ or "toml_dataclass")
|
|
65
|
+
|
|
66
|
+
__all__ = [
|
|
67
|
+
"JsonDataclass", "TomlDataclass",
|
|
68
|
+
"json_config", "toml_config",
|
|
69
|
+
"TypeChecker",
|
|
70
|
+
"__version__"
|
|
71
|
+
]
|