Warning
tomlantic is at 0.2.1 and currently, only i use it myself. it isn't battle tested,
so issues may arise.
if you're willing to run into potential bugs and issues, feel free to use it!
marrying pydantic models and tomlkit documents for data validated, style-preserving toml files
uses generics to automagically preserve model types, so you don't lose out on model type information :D
there are three notable methods to use tomlantic with your project:
-
install from pypi
pip install tomlantic
-
install from source
pip install git+https://github.com/markjoshwel/tomlantic
-
directly include tomlantic.py in your project
wget https://raw.githubusercontent.com/markjoshwel/tomlantic/refs/tags/v0.2.1/tomlantic/tomlantic.py
tomlantic is a single file module and is dually licenced for public domain dedication, or a public domain-equivalent licence. so, use it however you please!
see the licence section for more information.
import pydantic
import tomlkit
from tomlantic import ModelBoundTOML
class Project(pydantic.BaseModel):
model_config = pydantic.ConfigDict(validate_assignment=True)
name: str
description: str
typechecked: bool
class File(pydantic.BaseModel):
model_config = pydantic.ConfigDict(validate_assignment=True)
project: Project
toml_doc = tomlkit.parse(
"[project]\n"
'name = "tomlantic"\n'
'description = "marrying pydantic models and tomlkit documents"\n'
"typechecked = false # change this!\n"
)
# where tomlantic comes in handy
toml = ModelBoundTOML(File, toml_doc)
# access the model with .model and change the model freely
model: File = toml.model
model.project.typechecked = True
# dump the model back to a toml document
new_toml_doc = toml.model_dump_toml()
assert new_toml_doc["project"]["typechecked"] == True # type: ignore
print(new_toml_doc.as_string())
# ^^ [project]
# name = "tomlantic"
# description = "marrying pydantic models and tomlkit documents"
# typechecked = true # change this!Important
0.2.2 is currently in development. please see documentation as during the 0.2.1 branch for more information.
TODO
tomlantic is dually licensed under the Unlicense and the 0BSD License.
for more information, please refer to LICENCING.