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.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.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.3.0.tar.gz (100.4 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.3.0-cp310-abi3-win_amd64.whl (542.8 kB view details)

Uploaded CPython 3.10+Windows x86-64

yamltrip-0.3.0-cp310-abi3-musllinux_1_2_x86_64.whl (962.3 kB view details)

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

yamltrip-0.3.0-cp310-abi3-musllinux_1_2_aarch64.whl (920.9 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

yamltrip-0.3.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (781.2 kB view details)

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

yamltrip-0.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (741.2 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

yamltrip-0.3.0-cp310-abi3-macosx_11_0_arm64.whl (680.1 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

yamltrip-0.3.0-cp310-abi3-macosx_10_12_x86_64.whl (697.5 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: yamltrip-0.3.0.tar.gz
  • Upload date:
  • Size: 100.4 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.3.0.tar.gz
Algorithm Hash digest
SHA256 3cf3693773d0a4bd0137b04f0ed3ecae918727bcc2afef803ac363038adb3522
MD5 b5023fbb3feea9bf94018254202a64a5
BLAKE2b-256 cfb51ceeafd84c90ee7cfd202d4eb0145675c80a2c5d7cc74fd7ce9f474ca58a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.3.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 542.8 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.3.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d4df1c082edf35c6db7ac0aa5247d2b1086ed0c8b6efaaf05a730d251c53197a
MD5 3ee2c29e581841372831d1a110f688e3
BLAKE2b-256 18c13ec261517f8ea2d2d79cece8977c006cf15e79af4e05ab843390669ffdcb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.3.0-cp310-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 962.3 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.3.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7427e860bcc6f069592d27e145f140e9b2c194ac482c4bbca852776b0dc4192a
MD5 f3e84c7e6b6e5fdfc3d1fc7505a4e754
BLAKE2b-256 cc164d75c429b2e7dd35996063f981e00dae2c07e6e5a04ffa826c6fd7d4765f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.3.0-cp310-abi3-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 920.9 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.3.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 10fb296d0d921fdeed9dfa35267eab819a3ecf230621495206b663da7febe414
MD5 318bcc512efc8f2e77aca2b4622ea11d
BLAKE2b-256 7ef21711b55ae26dbd62d889e11359eaaedc094b9c29b944e311b2e8e5578d11

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.3.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 781.2 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.3.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bb9a402c49df159cb263f2f374ed4dc986ee15723e8bf5cb3e0d0c003cc59fc9
MD5 217173880781f9c6214000bf3dd03cdd
BLAKE2b-256 345688ccee1e383c7fff5ecf08516f465a4a1ca7b16ebafc1384ea2024597ec2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 741.2 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.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c85458a7403cf7986384279a1bdb0c7058fc9e75340ab8cdcbabbf3e1b9e1db8
MD5 e37f4d62603406a854140059397284ef
BLAKE2b-256 bdf6bfb3211b197192faee1be3501b0982da3b77d48dca3141bb6bc35742d992

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.3.0-cp310-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 680.1 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.3.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7239a891fc5b2e409ace1d056b3acfec0e457a3371de03e9bea4f878c7bb1406
MD5 332262a658378522a60b16deb60b63fa
BLAKE2b-256 a8d83ec065a25964f2bfaea25668b0301997c5d83dd4ac21133460ec3fd8ed0d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamltrip-0.3.0-cp310-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 697.5 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.3.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7ae60df07faea25ba9d7267c7f13165180bc4a89cb292df2f8219609503f0a45
MD5 173d826086841e766631067b7f91c7c0
BLAKE2b-256 2ced232b16fffcf11892b434443c7e8fdbfc5359140d8d31a140d04be7ba86e5

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