Skip to main content

A modern YAML 1.2 parser and emitter for Python, written in Rust.

Project description

yaml12

A YAML 1.2 parser/formatter for Python, implemented in Rust for speed and correctness. Built on the excellent saphyr crate.

For almost every use case, yaml12 lets you work with plain builtin Python types end to end: dict, list, int, float, str, and None. JSON is a subset of YAML 1.2, so all valid JSON is also valid YAML and parses the same way.

  • Parse YAML text or files with parse_yaml() and read_yaml().
  • Serialize Python values with format_yaml() or write_yaml().
  • 100% compliance with the yaml-test-suite.
  • Advanced YAML features (document streams, tags, complex mapping keys) are supported and round-trip cleanly when needed. Yaml is the wrapper type for tagged nodes and unhashable mapping keys.

Installation

The package ships prebuilt wheels for Python 3.10+ on common platforms. Install from PyPI:

pip install py-yaml12

Development install

You can install the development version of yaml12 from github. Clone the repository and install in editable mode:

git clone https://github.com/posit-dev/py-yaml12.git
cd py-yaml12
python -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -e . --no-build-isolation

To install the latest main branch without cloning:

pip install git+https://github.com/posit-dev/py-yaml12.git

Python 3.10+ and a Rust toolchain are required.

Quick start

from yaml12 import parse_yaml, format_yaml, Yaml

yaml_text = """
title: A modern YAML parser and emitter written in Rust
properties: [fast, correct, safe, simple]
features:
  tags: preserve
  streams: multi
"""

doc = parse_yaml(yaml_text)

assert doc == {
    "title": "A modern YAML parser and emitter written in Rust",
    "properties": ["fast", "correct", "safe", "simple"],
    "features": {"tags": "preserve", "streams": "multi"},
}

round_tripped = parse_yaml(format_yaml(doc))
assert round_tripped == doc

# Tagged values (advanced)
from yaml12 import Yaml

tagged = parse_yaml("!expr 1 + 1")
assert tagged == Yaml(value="1 + 1", tag="!expr")

Reading and writing files

from yaml12 import read_yaml, write_yaml

value_out = {"alpha": 1, "nested": [True, None]}

write_yaml(value_out, "my.yaml")
value_in = read_yaml("my.yaml")
assert value_in == value_out

# Multi-document streams
docs_out = [{"foo": 1}, {"bar": [2, None]}]
write_yaml(docs_out, "my-multi.yaml", multi=True)
docs_in = read_yaml("my-multi.yaml", multi=True)
assert docs_in == docs_out

Tag handlers

Handlers let you opt into custom behavior for tagged nodes while keeping the default parser strict and safe.

from yaml12 import parse_yaml

yaml_text = """
- !upper [rust, python]
- !expr 6 * 7
"""

handlers = {
    "!expr": lambda value: eval(value),
    "!upper": lambda value: [x.upper() for x in value],
}

doc = parse_yaml(yaml_text, handlers=handlers)
assert doc == [["RUST", "PYTHON"], 42]

Non-string mapping keys and tags

YAML mappings can use keys that themselves collections, or that carry tags. Such keys cannot always be represented directly in a Python dict, so yaml12 wraps them in Yaml to make the key hashable.

from yaml12 import Yaml, parse_yaml, format_yaml

obj = {
    "seq": [1, 2],
    "map": {"key": "value"},
    "tagged": Yaml("1 + 1", "!expr"),
    Yaml("foo", "!custom-key"): "bar",
}

yaml_text = format_yaml(obj)
round_tripped = parse_yaml(yaml_text)
assert round_tripped == obj

Documentation

Online docs: https://posit-dev.github.io/py-yaml12/

  • Guides: docs/usage.md (basics) and docs/tags.md (advanced tags, anchors, streams).
  • Reference: docs/reference/.
  • API overview: docs/api.md.

To build or serve the docs locally:

.venv/bin/mkdocs build        # or: .venv/bin/mkdocs serve

Tests

From the repo root:

cargo fmt
cargo check
cargo test
cargo build
cargo clippy
.venv/bin/pip install -e . --no-build-isolation
.venv/bin/python -m pytest tests_py

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

py_yaml12-0.1.0.tar.gz (117.2 kB view details)

Uploaded Source

Built Distributions

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

py_yaml12-0.1.0-pp311-pypy311_pp73-win_amd64.whl (360.0 kB view details)

Uploaded PyPyWindows x86-64

py_yaml12-0.1.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (513.9 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

py_yaml12-0.1.0-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl (553.8 kB view details)

Uploaded PyPymanylinux: glibc 2.5+ i686

py_yaml12-0.1.0-pp311-pypy311_pp73-macosx_11_0_arm64.whl (466.3 kB view details)

Uploaded PyPymacOS 11.0+ ARM64

py_yaml12-0.1.0-cp310-abi3-win_amd64.whl (362.3 kB view details)

Uploaded CPython 3.10+Windows x86-64

py_yaml12-0.1.0-cp310-abi3-win32.whl (349.6 kB view details)

Uploaded CPython 3.10+Windows x86

py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl (719.4 kB view details)

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

py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_i686.whl (764.7 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ i686

py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_armv7l.whl (789.7 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl (683.7 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (516.9 kB view details)

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

py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (530.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (640.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (519.7 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (501.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

py_yaml12-0.1.0-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl (557.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.5+ i686

py_yaml12-0.1.0-cp310-abi3-macosx_11_0_arm64.whl (468.7 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file py_yaml12-0.1.0.tar.gz.

File metadata

  • Download URL: py_yaml12-0.1.0.tar.gz
  • Upload date:
  • Size: 117.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for py_yaml12-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e7b6140fad56d5528e6d51c7b8ff79bdb27631b73a9a7c1bc4b55fda1bddbaef
MD5 a9811aa0123a5e1dc5492f6d8f2dcc08
BLAKE2b-256 d4eba0b260f7b8625d21c7a900311c863e899d2e258ebdc9a056a0cd17031f80

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-pp311-pypy311_pp73-win_amd64.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-pp311-pypy311_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 9a8fedadf47b35da5224e26936ddc26ed379fa0a184aaff709cbb75a2cf0df21
MD5 b3b8bcb4cc020fab759715273b4f551b
BLAKE2b-256 363f6f09b4f5384b9b0a1d42a0945641a89439b1fe5becc27de064ae56c8059c

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0720e3bd829d9887a7ce28e74e727af4e2c17c896b294d65c131361ba0767768
MD5 0b709f72e6a806e882718f6d6eb006ab
BLAKE2b-256 2b0eec5ae405508f2cb6628ad74b6af8bbd1b87ffac432db1b70ffc621e96003

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 90baa98b34c82660f5d856c54ee6c8505ab2428e72998f487972fd5ad0c40701
MD5 abbc14a879154c3d596850bcc68da487
BLAKE2b-256 a4269ccd16342fa44e55d6351da32d3718daada5aed6037469f4c3b9c7f79363

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-pp311-pypy311_pp73-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-pp311-pypy311_pp73-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f874ca279b1c58ec6a4853876a0e51b51e3a6d3474168a15529fd68fbf268ca7
MD5 1a101ab61f5d58fe784aa47b9b25e680
BLAKE2b-256 f19ca2cc38da0d06fcf7c13c12e14208f933636a2d51c3e9867e120cfe4352d1

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: py_yaml12-0.1.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 362.3 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 228ce36885a16dcaf20007bf7b74ae1b36ccd20a46149f369e4b8164e7542023
MD5 b4b46f922504d3bcb272300153481c56
BLAKE2b-256 4860d50a1e5e44333aa6533080df9c587b6b25e0205071078c63619202592bb3

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-win32.whl.

File metadata

  • Download URL: py_yaml12-0.1.0-cp310-abi3-win32.whl
  • Upload date:
  • Size: 349.6 kB
  • Tags: CPython 3.10+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 2552c7957819f53b7d667baab38e59db066f067aafa6695849ae94c0f01bf999
MD5 46356190e617d8f29a0a83ddc7fe3bad
BLAKE2b-256 375beaa16c85074b44b1b6843d52fb3b75da26410dcea1e98cf6b6c4f46f27af

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 fcd21f854faf23bd1fff4822e7cf29a6e8b69dc43f69f6f2f34a698dcde4548b
MD5 850ea6e0df5476e8b8c41f4b36c052f4
BLAKE2b-256 81f5936eb310b0c84d044d92ccd90b99b53fb8326dc6084ce59fe47a6b78eb93

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 a29558b3ba9138eac75996dd0a92fda7993c085aa10c1c93b641239389b731be
MD5 9d96e364d08633d3c4778e665463a5b0
BLAKE2b-256 a918703812d770166e292e13388bf899f2540c83a8c9d6505862fe756b2818d1

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 0226a5aeba67b97ed8e2947da332e5c3b5ef004c0058d2cff2413c99fc4b9ff4
MD5 e5f73667eb9654bcd264c689c2250997
BLAKE2b-256 74b4f293b9fa7ad26b86278840f3c571a211111091fd03ba553fa45e4bcaf780

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 4c9fabfe8fdf53eb661eaaace95961dae742abe0679a9a989936e8b2cf893dbe
MD5 b041af1a67efcee3c25c2a80dc098fd2
BLAKE2b-256 7a41828743df93b97ca987a808895f5171f2430ee61f18f25fe3d9033075879d

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fbc5d54ec0471c0f64b931ea0907f655c728597b98a9e7f4a98f4c76cc3614ba
MD5 4807b39caa552878066be07f93d8f538
BLAKE2b-256 8e0c1e1b171246a44753bf04c6399179da1ccf99cfb9de1e086afd7326765b34

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 6845871e3a0233d71f75e56caca5c350f48cb5d9c50e6bb37874243be1ca5f2b
MD5 b3c9e0608e8d879d18790d028deb8ab4
BLAKE2b-256 a1a97be4ced3e490fd2a5e92cc8e8fe5b6143036a480ba004235e0a78ccbc6f9

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 003a9526a4731010f4e37eced9f33fd070ff563fe3bc2fe2416ab7598982b285
MD5 f4d2d9e0b466e29f1fb45f926b3c7280
BLAKE2b-256 2b734e1f7cdc26b1deb60ea42ca4b3717fab279a42df10f9b16b869550e92825

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 0e637189cb07e180bdc9db847ca5711b54df2233b55604bf3609cfa0f0228f7c
MD5 6c7c0e6b49abefba9a209bd4ed38bcd9
BLAKE2b-256 b605a8408ca1f4de61f8ef626391908006fa1c5e54eaf0f3288d78bc663d65aa

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 69b9c8e54afc415d4eb038a024bc9e28fed95b8c447708bf1a24da0291bbfdd8
MD5 881a481051ad3008a113c66b42b01e1c
BLAKE2b-256 4cabc08a62f92f69d45fc479b1e893968c9d9b2dc87809e538cea0409cdca25e

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 3d9f1ad2e166eb113fd4e44263baad929e268dc15abc7eb87736aa7148d07534
MD5 83dad2c2458e93fcc126a2cc0765c180
BLAKE2b-256 e300b4d1db376b35f0021c7e45b6351536c217426b32f71ad8b89782a3a6b562

See more details on using hashes here.

File details

Details for the file py_yaml12-0.1.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for py_yaml12-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f6dcce183b6744c0909a8440d1f7a00836023826e463601d03b879c0e84c8a19
MD5 df79fc5bd386378291993e896d3edce2
BLAKE2b-256 bed95f4ccd96d7c776e822e63d2b79e3594653bcf55720dbad1cdcbc18a170ad

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