Skip to main content

A round-tripping YAML library for Python

Project description

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())

# 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["items"]                  # ["a", "b"]
doc["items", 0]               # "a"
("items", 0) in doc           # True

doc.replace("items", 0, value="x")
doc.add("items", key="c", value=3)
doc.upsert("new", "nested", value=True)
doc.remove("items", 0)
doc.prune_remove("a", "b", "c")  # remove + prune empty parents
doc.append("items", value="c")
doc.extend_list("items", values=["d", "e"])
doc.remove_from_list("items", values=["a"])

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")
    print(ed["version"])        # "2.0"
    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.

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. Python-style negative indexing is not supported.
  • 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yamltrip-0.2.0.tar.gz (95.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

yamltrip-0.2.0-cp310-abi3-win_amd64.whl (534.6 kB view details)

Uploaded CPython 3.10+Windows x86-64

yamltrip-0.2.0-cp310-abi3-musllinux_1_2_x86_64.whl (945.7 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

yamltrip-0.2.0-cp310-abi3-musllinux_1_2_aarch64.whl (905.3 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

yamltrip-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (765.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

yamltrip-0.2.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (726.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

yamltrip-0.2.0-cp310-abi3-macosx_11_0_arm64.whl (665.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

yamltrip-0.2.0-cp310-abi3-macosx_10_12_x86_64.whl (682.6 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file yamltrip-0.2.0.tar.gz.

File metadata

  • Download URL: yamltrip-0.2.0.tar.gz
  • Upload date:
  • Size: 95.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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}

File hashes

Hashes for yamltrip-0.2.0.tar.gz
Algorithm Hash digest
SHA256 37b17e69a1f5b7d998d4ab05e2f78f34b63167d42c411f3278b53cd1504fc96c
MD5 86f16afa65c3944f247aa9fa50d3ccc7
BLAKE2b-256 12128d490dfdf34520b168cf1f73dc4c44a5a7410448b932fecff36606295d5a

See more details on using hashes here.

File details

Details for the file yamltrip-0.2.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: yamltrip-0.2.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 534.6 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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}

File hashes

Hashes for yamltrip-0.2.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 99282a6fc5923c07ba007b5774eb164fdb8670d308a55624ad72c1cc5d3b3562
MD5 fe51c6174e73dcc6675353f8feec73be
BLAKE2b-256 46f4da5cf40b71aa3d21d1498cc1909f8772a20f5450fea1f77a8cc4ba81555e

See more details on using hashes here.

File details

Details for the file yamltrip-0.2.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: yamltrip-0.2.0-cp310-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 945.7 kB
  • Tags: CPython 3.10+, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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}

File hashes

Hashes for yamltrip-0.2.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 45963576fe6e6309a5d5167e6efe8821fe568c084109ca279f95b20f4fccd328
MD5 f0e46f37e029bab1ca928f1a816062f8
BLAKE2b-256 2291c5859bb8b66a0444df823fb30a41b944c339639caa587a71037f10771543

See more details on using hashes here.

File details

Details for the file yamltrip-0.2.0-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

  • Download URL: yamltrip-0.2.0-cp310-abi3-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 905.3 kB
  • Tags: CPython 3.10+, musllinux: musl 1.2+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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}

File hashes

Hashes for yamltrip-0.2.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 ea44bc843b71b1d515f958770d73646f41530d11d6eea64dd03e868f04bb0223
MD5 e418372924b75210093ebe18f9d470ae
BLAKE2b-256 4345507c17bae4ed4e3c5b8248b99740d7f7989229015f1a46cc0219eeb4d297

See more details on using hashes here.

File details

Details for the file yamltrip-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: yamltrip-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 765.0 kB
  • Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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}

File hashes

Hashes for yamltrip-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 99d67a58798ba9e2a5a20ec6bf08ef868aa4e3b72030183e32e66446e590da53
MD5 d0fd03060036075a10ef7725832c599f
BLAKE2b-256 b53d088257de81811fdcc1014c56da8fb78bb97f6d37449ce965f7ce7d754dfe

See more details on using hashes here.

File details

Details for the file yamltrip-0.2.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

  • Download URL: yamltrip-0.2.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 726.3 kB
  • Tags: CPython 3.10+, manylinux: glibc 2.17+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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}

File hashes

Hashes for yamltrip-0.2.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3e50899f21f2e2d82b464c1fbe78a0136ddff77995e8d5655b61d2f6ffbff560
MD5 b410d72f19a87f1f5d023587591db631
BLAKE2b-256 ddf7fb20cfb6fa8f09022e3765a44faebd1f4081691f8e7e0d16a615c18d592b

See more details on using hashes here.

File details

Details for the file yamltrip-0.2.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: yamltrip-0.2.0-cp310-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 665.9 kB
  • Tags: CPython 3.10+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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}

File hashes

Hashes for yamltrip-0.2.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 226dc8fb3bca588d823ea8a449c180d44d003e97db6985b85395deee946fdb38
MD5 c2076da666abdddc4679e87dcbaa1122
BLAKE2b-256 2025ec43e41c6a40358c48d3332ce43bbf721761054489ebf83b18c40e8896bd

See more details on using hashes here.

File details

Details for the file yamltrip-0.2.0-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: yamltrip-0.2.0-cp310-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 682.6 kB
  • Tags: CPython 3.10+, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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}

File hashes

Hashes for yamltrip-0.2.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 841dfcfa769f0ace208bc1c087c2934c39633332a1c6ee904ee7ad1e0a697525
MD5 e0a9227f20bf2b742fd4ad5fe77e3113
BLAKE2b-256 4368b7d84833740a0e483da19cc5c369c68bd28cda70670b319aac1da9a08053

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page