Style preserving TOML library for Python that maintains formatting while providing an intuitive API.
npx @tessl/cli install tessl/pypi-tomlkit@0.13.0A 1.0.0-compliant TOML library for Python that preserves all comments, indentations, whitespace and internal element ordering while making them accessible and editable via an intuitive API. TOML Kit enables programmatic modification of TOML files while preserving their original formatting and structure.
pip install tomlkitimport tomlkitFor direct access to main functions:
from tomlkit import parse, loads, load, dumps, dump, documentFor creating TOML items:
from tomlkit import integer, float_, boolean, string, array, table, inline_tableFor file operations:
from tomlkit.toml_file import TOMLFileimport tomlkit
# Parse existing TOML content
content = '''
# This is a TOML document
title = "Example"
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
'''
doc = tomlkit.parse(content)
print(doc["title"]) # "Example"
# Modify while preserving formatting
doc["title"] = "Updated Example"
doc["database"]["ports"].append(8003)
# Output preserves original formatting
print(doc.as_string())
# Create new TOML document from scratch
doc = tomlkit.document()
doc["title"] = "New Document"
doc["server"] = {"host": "localhost", "port": 8080}
print(tomlkit.dumps(doc))TOML Kit uses a layered architecture that separates parsing, item representation, and formatting:
This design enables round-trip parsing where input formatting is perfectly preserved while providing a familiar dict-like interface for programmatic access and modification.
Core functions for parsing TOML from strings/files and serializing back to TOML format, maintaining perfect formatting preservation.
def parse(string: str | bytes) -> TOMLDocument: ...
def loads(string: str | bytes) -> TOMLDocument: ...
def load(fp: IO[str] | IO[bytes]) -> TOMLDocument: ...
def dumps(data: Mapping, sort_keys: bool = False) -> str: ...
def dump(data: Mapping, fp: IO[str], *, sort_keys: bool = False) -> None: ...
def document() -> TOMLDocument: ...Functions for creating individual TOML items (integers, strings, tables, etc.) with proper type representation and formatting control.
def integer(raw: str | int) -> Integer: ...
def float_(raw: str | float) -> Float: ...
def boolean(raw: str) -> Bool: ...
def string(raw: str, *, literal: bool = False, multiline: bool = False, escape: bool = True) -> String: ...
def array(raw: str = "[]") -> Array: ...
def table(is_super_table: bool | None = None) -> Table: ...
def inline_table() -> InlineTable: ...Specialized TOML types including dates/times, keys, and complex data structures with full TOML 1.0.0 compliance.
def date(raw: str) -> Date: ...
def time(raw: str) -> Time: ...
def datetime(raw: str) -> DateTime: ...
def key(k: str | Iterable[str]) -> Key: ...
def value(raw: str) -> Item: ...
def aot() -> AoT: ...High-level interface for reading and writing TOML files with automatic encoding handling and line ending preservation.
class TOMLFile:
def __init__(self, path: StrPath) -> None: ...
def read(self) -> TOMLDocument: ...
def write(self, data: TOMLDocument) -> None: ...Core item classes representing different TOML value types, containers, and formatting elements with full type safety.
class TOMLDocument(Container): ...
class Container: ...
class Bool: ...
class Integer: ...
class Float: ...
class String: ...
class Array: ...
class Table: ...
class InlineTable: ...Comprehensive exception hierarchy for parsing errors, validation failures, and runtime issues with detailed error reporting.
class TOMLKitError(Exception): ...
class ParseError(ValueError, TOMLKitError): ...
class ConvertError(TypeError, ValueError, TOMLKitError): ...
class NonExistentKey(KeyError, TOMLKitError): ...
class KeyAlreadyPresent(TOMLKitError): ...# Type aliases for file paths
StrPath = Union[str, os.PathLike]
# Generic item type
Item = Union[Bool, Integer, Float, String, Date, Time, DateTime, Array, Table, InlineTable, AoT]
# Encoder function type
Encoder = Callable[[Any], Item]
# Document type
TOMLDocument = Container