A YAML 1.2 parser/formatter for Python, implemented in Rust for speed
and correctness. Built on the excellent
saphyr crate.
Compared with YAML 1.1's type library, YAML 1.2's recommended core
schema uses more conservative type inference for unquoted values. The
YAML 1.1 versus 1.2 quick
reference
summarizes the differences.
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().
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 CPython 3.10+ and PyPy 3.11. Install from PyPI:
pip install py-yaml12
Wheel coverage:
Platform
CPython architectures
PyPy 3.11 architectures
Linux (glibc)
x86-64, x86, ARM64, ARMv7, ppc64le, s390x
x86-64, x86
Linux (musl)
x86-64, x86, ARM64, ARMv7
—
macOS
Apple silicon, Intel
Apple silicon, Intel
Windows
x86-64, x86, ARM64
x86-64
CPython 3.14 and 3.15 free-threaded builds use separate cp314t and
cp315t wheels for the same CPython platforms. Free-threading support
is currently beta.
Development install
You can install the development version of yaml12 from github.
Clone the repository and install in editable mode:
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.