Skip to main content

tomlgraft - Small edits. Clean diffs.

tomlgraft

Give your configuration a smaller diff.

Small TOML edits from Python, backed by the same Rust core as the CLI. Update configuration values, pyproject.toml dependencies, and PEP 723 inline script metadata while preserving untouched bytes, comments, and formatting. Edits are validated as a batch before any file is replaced.

Installation

python -m pip install tomlgraft==0.1.1
# or with uv:
uv add tomlgraft==0.1.1

Requires CPython 3.12. Prebuilt wheels support Windows x86-64 and Linux x86-64 with glibc 2.35+. Source builds require Rust 1.89+ and a platform linker. The extension requires the GIL; no universal ABI or free-threaded Python support is advertised.

The PyPI package installs Python bindings (import tomlgraft). Install the standalone CLI separately with cargo install tomlgraft --version 0.1.1 --locked or use the binaries from GitHub Releases.

Quickstart

Edit bytes in memory

patch() takes source bytes and returns output bytes plus a report:

import tomlgraft

source = b"[project]\nversion = '1.0.0' # keep this comment\n"
result = tomlgraft.patch(
    source,
    kind="pyproject",
    operations=[{"op": "set", "path": ["project", "version"], "value_toml": "'1.1.0'"}],
)
assert result["output"] == b"[project]\nversion = '1.1.0' # keep this comment\n"

Use kind="toml" for ordinary TOML, "pyproject" for project metadata, or "script" for PEP 723 metadata. Recognized lockfiles remain read-only. Paths are lists of literal key segments: ["tool", "example.org", "enabled"] selects a key containing a dot. value_toml is a TOML literal string, such as "true", "42", or "'1.1.0'".

Preview or write an existing file

patch_file() accepts a string or PathLike path, infers the document kind from the filename, and returns a report:

import tomlgraft

report = tomlgraft.patch_file(
    "pyproject.toml",
    operations=[
        {"op": "set", "path": ["project", "requires-python"], "value_toml": "'>=3.12'"},
        {"op": "dep_upsert", "scope": {"kind": "group", "name": "dev"}, "requirement": "pytest>=8"},
    ],
    dry_run=True,
)
print(report["status"], report["committed"])

Preview creates no lock sidecar or staging file. Set dry_run=False to apply the batch; file writes default to dry_run=False. Cooperating writers use a sibling lock, stage the validated output, and recheck the target before atomic replacement on supported local filesystems.

Manage Python dependencies

Use dep_upsert to add or replace a requirement and dep_remove to remove one:

import tomlgraft

result = tomlgraft.patch(
    b'[project]\ndependencies = ["requests>=2.30"]\n',
    kind="pyproject",
    operations=[
        {"op": "dep_upsert", "scope": {"kind": "project"}, "requirement": "httpx>=0.27"},
        {"op": "dep_remove", "scope": {"kind": "project"}, "selector": "requests"},
    ],
)
assert result["output"] == b'[project]\ndependencies = ["httpx>=0.27"]\n'
Dependency location Scope
project.dependencies {"kind": "project"}
project.optional-dependencies.test {"kind": "optional", "name": "test"}
dependency-groups.dev {"kind": "group", "name": "dev"}
PEP 723 script dependencies {"kind": "script"}

Matching uses normalized package names, sorted normalized extras, and parsed environment-marker structure. Version specifiers or URLs are replaced in full; distinct marker and extra variants remain separate. No dependency resolver or installer runs.

Add PEP 723 script metadata

Creating a missing # /// script block requires create_script_block=True. Both APIs default to False. The Python script is never executed:

import tomlgraft

source = b"print('hello')\n"
result = tomlgraft.patch(
    source,
    kind="script",
    operations=[
        {"op": "dep_upsert", "scope": {"kind": "script"}, "requirement": "rich>=13"},
    ],
    create_script_block=True,
)
assert result["output"].startswith(b"# /// script\n")
assert result["output"].endswith(source)

Preservation and validation

What you need What tomlgraft does
Focused diffs Preserves bytes, comments, key order, quote styles, LF/CRLF, and final-newline state outside the edit. Whole-document serialization is never a fallback.
Ordered batches Prepares and validates all operations in memory. A failed operation produces no partial output or target write.
Protected metadata Refuses changes to fields remaining in project.dynamic and treats recognized pylock.toml files as read-only.
Explicit refusals Rejects ambiguous layouts, unsupported edits, and file metadata it cannot preserve.
Local operation Runs no Python scripts, build backends, resolvers, subprocesses, or network requests.

set, remove, dep_upsert, and dep_remove work through both APIs. Removing a leaf also removes its same-line comment; standalone comments remain. Table deletion and arrays of tables are unsupported. See the preservation contract for supported layouts, dependency identity, and script discovery.

Error handling and file safety

Core errors inherit from tomlgraft.PatchError: ArgumentError, DocumentError, ConflictError, UnsupportedError, and TransactionError. They expose code, zero-based operation (when available), committed, durability, exit_code, and report.

Inspect committed and durability in file reports and exceptions. A failure after replacement can report committed=True; after interrupted or lost output, reread the target before retrying.

This is an experimental release. Writes are qualified only for existing regular files on local Windows x86-64/NTFS and Linux x86-64/ext4, within the documented metadata restrictions. Other platforms/filesystems support in-memory edits and previews; direct writes are refused. Windows directory durability is uncertain, and noncooperating editors can race the final recheck/rename. See the file transaction contract.

Performance

Recorded local medians for Python in-memory preparation of a 500-line manifest were 0.083 ms on Linux/WSL2 and 0.087 ms on Windows (30 samples per platform, AMD Ryzen 7 9800X3D, CPython 3.12, Rust 1.97 release builds). These include binding conversion and preparation, exclude file I/O, and are descriptive measurements rather than latency guarantees. See methodology and raw samples.

Documentation

Metadata

Release files for tomlgraft 0.1.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 tomlgraft 0.1.1
File Size Uploaded Provenance Actions
tomlgraft-0.1.1.tar.gz 40.4 kB With provenance Details for tomlgraft-0.1.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for tomlgraft 0.1.1
File Interpreter ABI Platform Actions
tomlgraft-0.1.1-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for tomlgraft-0.1.1-cp312-cp312-win_amd64.whl
tomlgraft-0.1.1-cp312-cp312-manylinux_2_35_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.35+ x86-64 Details for tomlgraft-0.1.1-cp312-cp312-manylinux_2_35_x86_64.whl

Total release size: 3.1 MB

Release files / tomlgraft-0.1.1.tar.gz

Download URL tomlgraft-0.1.1.tar.gz
Size 40.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3283fec06d2a095a51a2a3c89313710a057c1bcf943c982da20640ca7ccdd0ae
BLAKE2b-256 checksum
How to use checksums
b97311b4441e91013a77909f7d18596d1eb359cd4979a4882c98a4a3a47733e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

Release files / tomlgraft-0.1.1-cp312-cp312-win_amd64.whl

Download URL tomlgraft-0.1.1-cp312-cp312-win_amd64.whl
Size 1.5 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
644436d63a9608174bc1ed89f6b45f2ad2181cf66305c2e39cf07da2fc39b2aa
BLAKE2b-256 checksum
How to use checksums
142fdcd694a44654ba9ec22789c546e51d29f2c467fa929f6a798ef337a6110b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

Release files / tomlgraft-0.1.1-cp312-cp312-manylinux_2_35_x86_64.whl

Download URL tomlgraft-0.1.1-cp312-cp312-manylinux_2_35_x86_64.whl
Size 1.6 MB
Tags CPython 3.12 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
7b8043a5d2d16f89b0ff10b4b34c56c1634ed8f4e7389c157b5c7a0d53d332d9
BLAKE2b-256 checksum
How to use checksums
ef6fb7c3a40ba298aa3aa76b6684eb19a67d0ddf6d7f2a02c74976fe4b3889c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

Security for tomlgraft 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 3 files in this release have been uploaded with provenance metadata.

Select from 3 files

Attestation for tomlgraft-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

3 release files Full provenance

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