Skip to content

API reference

Import these functions and classes from tomledit. For examples of ordinary dictionary and list operations, see Working with items.

Reading and writing

load

load(fp: IO[bytes]) -> Document

Parse TOML from a file into a Document.

fp must be opened in binary mode (e.g. open(path, "rb")).

loads

loads(text: str) -> Document

Parse a TOML string into a Document, preserving formatting.

dump

dump(obj: Mapping[str, Any], fp: IO[bytes]) -> None

Serialise a Document or Mapping to a file.

fp must be opened in binary mode (e.g. open(path, "wb")).

dumps

dumps(obj: Mapping[str, Any]) -> str

Serialise a Document or Mapping to a TOML string.

Passing a Document preserves its formatting; passing any other mapping produces freshly-formatted output.

Documents and items

Document

A TOML document that preserves formatting when edited.

Create an empty document with Document(), or from a dict with Document({"key": "value"}). To round-trip an existing TOML file use tomledit.loads(text) which retains comments and whitespace.

value

value: dict[str, Any]

The entire document as a native Python dict.

as_toml

as_toml() -> str

Return the document serialised as a TOML string.

fmt

fmt() -> None

Normalize formatting of the document's top-level entries.

This only reformats root-level key/value entries. It does not recurse into nested tables or arrays; call .fmt() on a nested Item if you want to reformat that value specifically. Useful after mutations that leave awkward top-level whitespace. Note: comments on formatted root-level entries are removed.

Item

A live reference to a TOML value inside a Document.

Items are obtained by indexing a Document or another Item:

port = doc["server"]["port"]
doc["server"]["port"] = 8080

Every Item is one of three concrete subtypes — DictItem for tables, ListItem for arrays, or ScalarItem for plain values — which can be narrowed with isinstance if needed. Without narrowing, the full set of dict-like, list-like, and scalar methods is available directly on Item and will raise at runtime if called on the wrong kind.

Under the hood, an Item is really a path into the Document. An Item can become stale when a mutation to the Document changes the value that the Item points at. Using a stale Item raises a RuntimeError.

value

value: Any

The underlying data as a native Python object (int, str, list, dict, etc).

comment

comment: str | None

The comment lines before this entry, or None.

inline_comment

inline_comment: str | None

The inline comment after this value (e.g. key = 1 # this part), or None.

parse

parse(text: str) -> Item

Parse a TOML value fragment, preserving its representation.

as_toml

as_toml() -> str

Return the TOML representation of this item.

fmt

fmt() -> None

Normalize formatting of this item (spacing, trailing commas, etc.).

Useful after mutations that leave behind awkward whitespace. This is shallow - it formats the item itself, not nested sub-tables. Note: any comments on the formatted item will be removed.

DictItem

A TOML table or inline table.

isinstance(item, DictItem) and isinstance(item, MutableMapping) both work.

implicit

implicit: bool

Whether this table can be written without its own header.

For example, [a.b] creates a parent table a even when the file has no [a] header. Inline tables always return False.

Setting to True suppresses the header only if the table has no values of its own. Setting to False makes the header explicit. Assignments are ignored on inline tables.

ListItem

A TOML array or array of tables.

isinstance(item, ListItem) and isinstance(item, MutableSequence) both work.

set_multiline

set_multiline(*, indent: int = 4) -> None

Format the array as multiline.

Each element starts on its own line, indented by indent spaces, with a trailing comma after the last element. Use .fmt() to remove line breaks between elements.

Preserves comments. No-op on empty arrays and arrays of tables.

ScalarItem

A scalar TOML value (string, integer, float, boolean, datetime, date, or time).