Skip to main content

Build Status

PyPI version

tomli-w-null

A lil' TOML writer with support for non-standard null values (fork of tomli-w)

Table of Contents generated with mdformat-toc

Intro

tomli-w-null is a Python library for writing TOML, based on tomli-w. It extends the writer with support for the null value, mapping it from Python's None. All other features of tomli-w are preserved. It is a write-only counterpart to tomli-null, which is a read-only TOML parser.

tomli-w-null can produce TOML code based on TOML v1.0.0 (default) or v1.1.0. Since TOML 1.1.0 is fully backwards compatible with TOML 1.0.0 and doesn't introduce any new features in supported data structures, TOML 1.0.0 remains the default.

Installation

pip install tomli-w-null

Usage

Write to string

import tomli_w_null

doc = {"table": {"nested": {}, "val3": 3}, "val2": 2, "val1": 1}
expected_toml = """\
val2 = 2
val1 = 1

[table]
val3 = 3

[table.nested]
"""
assert tomli_w_null.dumps(doc) == expected_toml

Write to file

import tomli_w_null

doc = {"one": 1, "two": 2, "pi": 3}
with open("path_to_file/conf.toml", "wb") as f:
    tomli_w_null.dump(doc, f)

Specify TOML version

import tomli_w_null

doc = {"key": "value", "from_tty": "data\x04", "binary": "\x1a\x1b\x1c\x1d"}
expected_toml = """\
key = value
from_tty = "data\x04"
binary = "\x1a\e\x1c\x1d"
"""
assert tomli_w_null.dumps(doc, toml_version="1.1.0") == expected_toml

Write data with null values

import tomli_null

doc = {"value": None, "items": [1, None, 3]}
expected_toml = """\
value = null
items = [1, null, 3]
"""
assert tomli_w_null.dumps(doc) == expected_toml

Python's None is mapped to the null keyword (always produced lowercase, just like true / false in standard TOML).

FAQ

Does tomli-w-null sort the document?

No, but it respects sort order of the input data, so one could sort the content of the dict (recursively) before calling tomli_w_null.dumps.

Does tomli-w-null support writing documents with comments?

No.

Can I customize insignificant whitespace?

Indent width of array content can be configured via the indent keyword argument. indent takes a non-negative integer, defaulting to 4.

import tomli_w_null

doc = {"fruits": ["orange", "kiwi", "papaya"]}
expected_toml = """\
fruits = [
 "orange",
 "kiwi",
 "papaya",
]
"""
assert tomli_w_null.dumps(doc, indent=1) == expected_toml

Why does tomli-w-null not write a multi-line string if the string value contains newlines?

This default was chosen to achieve lossless parse/write round-trips.

TOML strings can contain newlines where exact bytes matter, e.g.

s = "here's a newline\r\n"

TOML strings also can contain newlines where exact byte representation is not relevant, e.g.

s = """here's a newline
"""

A parse/write round-trip that converts the former example to the latter does not preserve the original newline byte sequence. This is why Tomli-W avoids writing multi-line strings.

A keyword argument is provided for users who do not need newline bytes to be preserved:

import tomli_w_null

doc = {"s": "here's a newline\r\n"}
expected_toml = '''\
s = """
here's a newline
"""
'''
assert tomli_w_null.dumps(doc, multiline_strings=True) == expected_toml

Is tomli-w-null output guaranteed to be valid TOML?

No. If there's a chance that your input data is bad and you need output validation, parse the output string once with tomli.loads. If the parse is successful (does not raise tomli.TOMLDecodeError) then the string is valid TOML.

Examples of bad input data that can lead to writing invalid TOML without an error being raised include:

  • A mapping where keys behave very much like strings, but aren't. E.g. a tuple of strings of length 1.
  • A mapping where a value is a subclass of a supported type, but which overrides the __str__ method.

Given proper input (a mapping consisting of non-subclassed types returned by Tomli), the output should be valid TOML.

License

tomli-w-null is distributed under the terms of the MIT license, see LICENSE.

Release files for tomli-w-null 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tomli-w-null 1.0.1
File Size Uploaded
tomli_w_null-1.0.1.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tomli-w-null 1.0.1
File Interpreter ABI Platform
tomli_w_null-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.2 kB

Release files / tomli_w_null-1.0.1.tar.gz

Download URL tomli_w_null-1.0.1.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7ddda5950245725740626c0b2afd747b1d669dd017e98ed0badfe2b3ddf8e780
BLAKE2b-256 checksum
How to use checksums
54d67b04cf1df1c3ba03cd4733bd9998c18393ebb032f416163887c9b6434b36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / tomli_w_null-1.0.1-py3-none-any.whl

Download URL tomli_w_null-1.0.1-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0f2c12a21c093080de1d61b881f71554b854b47483a2c916b466f3ed52e0186
BLAKE2b-256 checksum
How to use checksums
a425b96793365df866b089e293a5a13927b79d20d87aad84a1f3b23a52fd4fcb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page