Skip to main content

yamltrip

usethis PyPI Version PyPI License PyPI Supported Versions

Edit YAML files from Python, while respecting format and comments during the round-trip.

Built on tree-sitter-yaml via the yamlpath and yamlpatch Rust crates.

Installation

# With uv
$ uv add yamltrip

# With pip
$ pip install yamltrip

Quick Start

import yamltrip

# Load and read
doc = yamltrip.loads("name: Alice\nage: 30")
print(doc["name"])       # "Alice"
print("name" in doc)     # True

# Immutable mutations: each call returns a new Document
doc2 = doc.replace("age", value=31)
doc3 = doc2.add(key="city", value="Portland")
print(doc3.dumps())

# Complex values (dicts/lists) work too
doc4 = doc3.replace("age", value={"years": 31, "months": 4})
doc5 = doc4.upsert("hobbies", value=["reading", "hiking"])

# File-based editing with a context manager
with yamltrip.edit("config.yml") as editor:
    editor.replace("version", value="2.0")
    editor.upsert("settings", "debug", value=True)
    # writes back on successful exit; discards on exception

API Overview

Top-level function

Function Description
yamltrip.loads(source) Parse a YAML string into a Document
yamltrip.load(path) Read a YAML file into a Document
yamltrip.edit(path) Open a YAML file for editing (context manager)

Document (immutable)

Every mutation method returns a new Document. The original is never modified.

doc = yamltrip.loads("items:\n  - a\n  - b")

doc.root                      # {"items": ["a", "b"]}
doc["items"]                  # ["a", "b"]
doc["items", 0]               # "a"
("items", 0) in doc           # True

doc.get("items", 0)               # "a" (returns None if missing)
doc.get("missing", default=42)    # 42

doc.replace("items", 0, value="x")
doc.replace("items", value=["x", "y"])  # dicts and lists accepted
doc.add("items", key="c", value=3)
doc.upsert("new", "nested", value=True)
doc.upsert("config", value={"debug": True})  # dicts and lists accepted
doc.remove("items", 0)
doc.prune_remove("a", "b", "c")  # remove + prune empty parents
doc.append("items", value="c")
doc.insert("items", index=1, value="between")  # positional insert
doc.extend_list("items", values=["d", "e"])
doc.remove_from_list("items", values=["a"])
doc.ensure_in_list("items", value="c")  # no-op if already present
doc.ensure_in_list("repos", where={"name": "x"}, value={"name": "x", "version": "1.0"})
doc.sync("items", value=["a", "new", "b"])  # minimal diff-and-patch
doc.merge("config", value={"debug": True, "level": 2})  # recursive mapping merge
doc.find_index("repos", where={"id": "x"})  # find in list-of-dicts; returns int | None

doc.query("items")            # Feature with location info
doc.query_pretty("items")    # Feature with surrounding context
doc.extract(feature)          # raw YAML text for a Feature
doc.has_anchors()             # True if anchors/aliases present
doc.dumps()                   # full YAML source
doc.dump("output.yml")        # write to file

Editor (mutable context manager)

Wraps Document with the same mutation methods, but applies changes in place and writes back to disk when the context exits cleanly:

with yamltrip.edit("config.yml") as ed:
    ed.replace("version", value="2.0")
    ed.upsert("new_key", value="new_value")
    ed.remove("old_key")
    ed.sync("deps", value={"a": "1.0", "b": "2.0"})  # minimal patching
    ed.merge("settings", value={"debug": True, "level": 2})  # recursive merge
    print(ed["version"])        # "2.0"
    print(ed.get("missing"))    # None
    print(ed.original["version"])  # original value before edits

Error Hierarchy

All yamltrip errors inherit from YAMLTripError:

  • ParseError: YAML input cannot be parsed.
  • QueryError: path not found during lookup.
  • PatchError: mutation operation failed.
    • KeyExistsError: add() target already exists.
    • KeyMissingError: replace() target does not exist.
    • RoutingError: path passes through a non-mapping node (scalar or list).
    • NodeTypeError (PatchError + TypeError): node is the wrong type for the operation (e.g. appending to a scalar).

Limitations

  • Multi-document YAML streams (--- separated) are not supported.
  • YAML tags (!!omap, !!set, !!merge, custom tags) are not interpreted.
  • Anchors and aliases (&anchor / *alias) are detected (doc.has_anchors()) but not resolved during value extraction.
  • Large integers may lose precision. YAML integers outside the signed 64-bit range (i64) may become float during deserialization.
  • Editor write-back is not atomic. Editor detects external file changes between enter and exit, but the check-then-write is racy. Do not use it with concurrent writers.

Design Decisions

  • No custom Python class serialization. Values convert to/from str, int, float, bool, None, list, and dict only.
  • UTF-8 only. Other encodings raise ParseError.
  • Non-finite floats round-trip. float("inf"), float("-inf"), and float("nan") map to YAML's .inf, -.inf, and .nan.
  • Integer keys cannot create structures. upsert() with integer path components can update existing sequence entries but cannot create new intermediate mappings. Only string keys create new mappings.
  • No negative sequence indices for lookup. Python-style negative indexing is not supported for [] access or replace()/remove(). However, insert() accepts negative indices (matching list.insert() semantics).
  • Line endings preserved as-is. No CRLF/LF normalization. Mixed line endings pass through unchanged.

Acknowledgements

yamltrip depends entirely on the yamlpath and yamlpatch Rust crates for format-preserving YAML parsing and patching. These libraries use tree-sitter to query and modify YAML source text without discarding comments, whitespace, or key ordering. All of the core logic in yamltrip passes through them. Thanks to William Woodruff for creating and maintaining both crates as part of zizmor.

License

MIT

Metadata

Release files for yamltrip 0.6.0

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

Source distribution (sdist)

Source distribution for yamltrip 0.6.0
File Size Uploaded
yamltrip-0.6.0.tar.gz 137.6 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for yamltrip 0.6.0
File
yamltrip-0.6.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
yamltrip-0.6.0-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details
yamltrip-0.6.0-cp310-abi3-musllinux_1_2_aarch64.whl CPython 3.10 abi3 Linux musl 1.2+ ARM64 Details
yamltrip-0.6.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details
yamltrip-0.6.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details
yamltrip-0.6.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details
yamltrip-0.6.0-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details

Total release size: 5.6 MB

Release files / yamltrip-0.6.0.tar.gz

Download URL yamltrip-0.6.0.tar.gz
Size 137.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2983877975ed10be6907a9cc7ef372f415bdcb2024dd91e2897e71dc2bba2b25
BLAKE2b-256 checksum
How to use checksums
ab0336404a8e2028ce4def0102eef142ccffbc74a1c12890c8d913c8e187bc41
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yamltrip-0.6.0-cp310-abi3-win_amd64.whl

Download URL yamltrip-0.6.0-cp310-abi3-win_amd64.whl
Size 556.8 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
11cba12d86fd5ed44fe2ce98a51e12fb60d4420ab2f1075ad8d744ee3beb0b2c
BLAKE2b-256 checksum
How to use checksums
c31e3f62eea5f06d4de4a17b583fccb63001eec942f06f2cf5fc05eaa47a6965
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yamltrip-0.6.0-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL yamltrip-0.6.0-cp310-abi3-musllinux_1_2_x86_64.whl
Size 976.7 kB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
b2d11277568743cacd397607663d75075115dd88a0bf4a4fad7fb0100dc5f612
BLAKE2b-256 checksum
How to use checksums
fde43b6ed066a75341da8b50f9ad49aec9d80b3812602dd29be3498a5da8c21d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yamltrip-0.6.0-cp310-abi3-musllinux_1_2_aarch64.whl

Download URL yamltrip-0.6.0-cp310-abi3-musllinux_1_2_aarch64.whl
Size 933.8 kB
Tags CPython 3.10 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
2054a7d9447829a4b531463c2454201e163e04341619614c8a3526e5a91f7b98
BLAKE2b-256 checksum
How to use checksums
05f01cb44f39c08e4101e55ec6662071dcfcc6660210039fcd2ff537bc7a808c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yamltrip-0.6.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL yamltrip-0.6.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 794.2 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
f11d97cddb255776f32888b8fcfce7707d39d83955c8202b067af7c17b270ce9
BLAKE2b-256 checksum
How to use checksums
69ca65fc1930f58b6381b0a43c9ebb8f385f576fa629113c039e0aa2d12b584f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yamltrip-0.6.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL yamltrip-0.6.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 755.3 kB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
985f8c0908764f6823999f2ce4916fdce5129bf6993b7df4bf1f0535379acdd4
BLAKE2b-256 checksum
How to use checksums
b909a751cf170298929edb00ff2eee67206dbbd76cf41076b9ed2b8bd5c78532
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yamltrip-0.6.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL yamltrip-0.6.0-cp310-abi3-macosx_11_0_arm64.whl
Size 693.9 kB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
08e9e9add38e2ea5fd14d5825897ebec065150d3c3a74c13a9b8db37778fa6a8
BLAKE2b-256 checksum
How to use checksums
d4cd3033674844b9bfdce057c8debf5b104f7004e1a7e21e55240b472b20e102
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yamltrip-0.6.0-cp310-abi3-macosx_10_12_x86_64.whl

Download URL yamltrip-0.6.0-cp310-abi3-macosx_10_12_x86_64.whl
Size 711.8 kB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
c58a90f9a5315af45d068458f4e0995fb7181df2b56c96da10bc9bca8284378d
BLAKE2b-256 checksum
How to use checksums
c7351f28e3d11603483b953f0e6f11dbd25f52525cb87d9407b97c04562e8ef5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.6.0 This release

8 release files

0.5.0

8 release files

0.4.0

8 release files

0.3.0

8 release files

0.2.0

8 release files

0.1.0

8 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