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

# 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.sync("items", value=["a", "new", "b"])  # minimal diff-and-patch

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
    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.

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

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.4.0.tar.gz (113.5 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.4.0-cp310-abi3-win_amd64.whl (552.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

yamltrip-0.4.0-cp310-abi3-musllinux_1_2_x86_64.whl (971.5 kB view details)

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

yamltrip-0.4.0-cp310-abi3-musllinux_1_2_aarch64.whl (927.2 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

yamltrip-0.4.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (789.5 kB view details)

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

yamltrip-0.4.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (748.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

yamltrip-0.4.0-cp310-abi3-macosx_11_0_arm64.whl (687.5 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

yamltrip-0.4.0-cp310-abi3-macosx_10_12_x86_64.whl (707.1 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: yamltrip-0.4.0.tar.gz
  • Upload date:
  • Size: 113.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.4.0.tar.gz
Algorithm Hash digest
SHA256 faa6a7c74a8f8ccc92114d8e9fc476042a7696e1fe44518f9b4f16c6a64c581f
MD5 928ff2a7776d7a953991b48587f8af0e
BLAKE2b-256 f2e0326f41c4e5ca508790e013b25209aacf95b33dfe3892f9144d7264e4f54a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.4.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 552.0 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.4.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 33d66c9bb01709982e447b0e0ebc4f15fcba55f7f408a6480ee9889eabbd2a88
MD5 5980e3841201aca2378c0b3fd45f525a
BLAKE2b-256 eebf0a90b6620fb2f0b35fbfb04869ef2e4f634332534e4ebfcbd6a0ce7bda2a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.4.0-cp310-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 971.5 kB
  • Tags: CPython 3.10+, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.4.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4a6c1f94967b59b6e125ebe144c73accda055689f6aa677b9b4ffec96dcd4864
MD5 5afa292fa3aa52a001fc88d33cf5a960
BLAKE2b-256 525ec0f25e9113f1ec55937c875d639625f32ba38b8d58475432d711821cb6cc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.4.0-cp310-abi3-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 927.2 kB
  • Tags: CPython 3.10+, musllinux: musl 1.2+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.4.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 235226133564b010bb4c7694a5767b7f2bd51d3d33c0ce21cb0c509fce29c84a
MD5 043a82c8ff16e4a3ce3b70a97528249f
BLAKE2b-256 2dc7df1d0934b2f84f2a9055f0207fb2bc23a71737aa9cb5aff8867a62a0c81a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.4.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 789.5 kB
  • Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.4.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a6ef52a06f7843be1756892e210b202d44aa0adab17dbb5f66b9968fb8aeb43f
MD5 70eb609692726ff2ef29fea6c4045a43
BLAKE2b-256 fd28ba0cb271a7e9ed6d87e6372fcbd25d14739e31779f61a1a9b3d5e99f7d11

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.4.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 748.0 kB
  • Tags: CPython 3.10+, manylinux: glibc 2.17+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.4.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1914e09d7bbb2705c31c2e51174807aea746ef1967ba4f7861dac69916dcbd20
MD5 9d9503ca02b0594a23e6a20e6e27f55e
BLAKE2b-256 c8d571b5cf6aada87fade50ce2edccffebed5a8d8286ac089196f62169600148

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.4.0-cp310-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 687.5 kB
  • Tags: CPython 3.10+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.4.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4377354a16020d5f6615bffd00b88280aa9dd1f667b1d6ba1fd188d36f352810
MD5 e42cb046704fb7585d8788998130548f
BLAKE2b-256 71a83c9b776928b414ab2d06c7638245600ea19b5686271748842b209a84ea24

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.4.0-cp310-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 707.1 kB
  • Tags: CPython 3.10+, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.4.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8197fe32dbf901f4c2fa732a2d56a2bbb852bad655f14ce03a26b9ff7aced67e
MD5 bc73274eb668508983c546b7261959e7
BLAKE2b-256 c993cb579a9f24088f7370e17025ca4dad686f6fbad72fdc25a30cba3b5f4fc4

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