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.5.0.tar.gz (117.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.5.0-cp310-abi3-win_amd64.whl (552.9 kB view details)

Uploaded CPython 3.10+Windows x86-64

yamltrip-0.5.0-cp310-abi3-musllinux_1_2_x86_64.whl (972.6 kB view details)

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

yamltrip-0.5.0-cp310-abi3-musllinux_1_2_aarch64.whl (928.1 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

yamltrip-0.5.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (790.4 kB view details)

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

yamltrip-0.5.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (748.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

yamltrip-0.5.0-cp310-abi3-macosx_11_0_arm64.whl (688.5 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

yamltrip-0.5.0-cp310-abi3-macosx_10_12_x86_64.whl (708.1 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: yamltrip-0.5.0.tar.gz
  • Upload date:
  • Size: 117.8 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.5.0.tar.gz
Algorithm Hash digest
SHA256 b66eaa7907867fe71ca056434de27339a9e12476e3ee81845eb8ae5077085e4c
MD5 09cd949fbc2b9545e4322c41af1adc90
BLAKE2b-256 d25cf949f6b03334c08380a7b8add1d41c823d6bdeec09bc77ce9f798197fa77

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.5.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 552.9 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.5.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 40e83f45ee9f98706d0f6b0dc6d205ba1f52189e6a5b4362f545816699782aef
MD5 824e6b8d5c1f96569072a7d622700473
BLAKE2b-256 beff8cc1df7310da6fea6f983b33d2fb667d071c8a8bfbb2e725824103666c51

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.5.0-cp310-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 972.6 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.5.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b1ccb2c9b1c99c51dde9eaebdb819f03fca17277208209091fc7250ee821af81
MD5 5224e86b299ec134d04442132331a85d
BLAKE2b-256 58a6fdf6770ebcf48acc0e5fe4f466cfbfef77b678ce63e712d77f350212bddd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.5.0-cp310-abi3-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 928.1 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.5.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5d162e0381ed3dcacf3cce92ff96d6e64a6f32d4e686e07ef06ad34de20caad1
MD5 1a8726b7252931df3608b6b548f3c8d9
BLAKE2b-256 e2643902108f52f34b3a35fcbf437e803c5e7ca0e41a6b568ab44354c795989a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.5.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 790.4 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.5.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 09ce6cb648fa4f134d689d26c35c591d823a7bc985c33c3399e6e6166b2c3a1b
MD5 c37709a8c7863b10f2c0a9f2fe957fe0
BLAKE2b-256 55b755251b2054a206cae25617e4afbff8b8fd3e69f3fe813f2ddd21fd78a616

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.5.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 748.9 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.5.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 deabe11b0fd2123f59dac698973ea6e65d1a9839967ac929ec8ecdb92365b7a8
MD5 3af73c75d0098c541f487587830190d4
BLAKE2b-256 cca845424789e1f5a7c055b1c04df8b0ad80fa5d1541366bf5d0a86fe62b277d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.5.0-cp310-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 688.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.5.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 62d7a6d716083e332513b6e7a85c15e2edfe5624057e04b4890018161bb2a32e
MD5 00b227cbba440b12afe6354ec2b489ca
BLAKE2b-256 a4a1c06384932cb129aff59852989698980c2bff931657c6b9d6b007b8f8ce7c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.5.0-cp310-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 708.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.5.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cc6c3ca4c035e2c882b8c353a3f3eeae18a718a63408c5d671acc8d7e07e5547
MD5 fc8f17798d7ca7ac4366f59d24ec284c
BLAKE2b-256 c3c6908cf090afbaf43defd4fd72a76b98bd7f5412823eb6b163dc00b9a20671

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