safer-yaml
A tiny wrapper around PyYAML and yamlcore that provides a consistent, JSON-like API for YAML 1.2.
Why
- Consistent API: mirrors Python’s json module: load, loads, dump, dumps.
- YAML 1.2 semantics: avoids the classic “Norway problem” and treats on/off/yes/no as plain strings unless explicitly boolean.
- Simple file/stream handling: accepts path-like objects and text streams, with UTF-8 file I/O by default.
- Minimal quoting and stable ordering: outputs human-friendly YAML with minimal necessary quotes and preserves insertion order.
Install
pip install safer-yaml
Quick start
from safer_yaml import load, loads, dump, dumps
# YAML 1.2 semantics (not YAML 1.1)
assert loads("no\n") == "no" # not False
assert loads("on\n") == "on" # not True
# Dump to a string
s = dumps({"k": "no"})
print(s) # k: no
Dump and load using a string filename
from safer_yaml import load, dump
dump({"a": "1:2", "b": "a: b"}, "myfile.yaml")
data = load("myfile.yaml")
assert data == {"a": "1:2", "b": "a: b"}
Use pathlib.Path too
from pathlib import Path
from safer_yaml import load, dump
p = Path("data.yaml")
dump({"a": "1:2", "b": "a: b"}, p)
data = load(p)
assert data == {"a": "1:2", "b": "a: b"}
Work with text streams
import io
from safer_yaml import dump
buf = io.StringIO()
dump({"outer": {"inner": "v"}}, buf)
print(buf.getvalue())
Control formatting (width and indent)
from safer_yaml import dumps
# Use defaults (no wrapping, indent=4)
print(dumps({"k": ["a", "b", "c"]}))
# Set your own
yaml_text = dumps({"nested": {"x": 1}}, width=80, indent=2)
print(yaml_text)
API by example
- loads
loads("k: v\n") # -> {"k": "v"}
- load
load("config.yaml") # path-like (opened UTF-8)
- dumps
dumps({"k": "v"}) # -> "k: v\n"
dumps({"k": "v"}, width=80, indent=2)
- dump
dump({"k": "v"}, "out.yaml")
with open("out.yaml", "r", encoding="utf-8") as f:
assert f.read().strip() == "k: v"
Defaults and constants
- DEFAULT_WIDTH = 1_000_000 (effectively disables wrapping)
- DEFAULT_INDENT = 4
If you need the constants:
from safer_yaml.safer_yaml import DEFAULT_WIDTH, DEFAULT_INDENT
Behavior and notes
- YAML 1.2 booleans: true/false are booleans; yes/no/on/off are strings by default.
- Minimal quoting: values are quoted only when required (e.g., "a: b", "#tag").
- Insertion order is preserved; keys are not sorted (sort_keys=False).
- Paths are handled cross-platform using os.fspath and PathLike.
- Uses yamlcore’s CCoreLoader/CCoreDumper for correctness and performance with YAML 1.2.
Compatibility
- Python: 3.9+
- Platforms: cross-platform (Windows, macOS, Linux)
License MIT — see LICENSE.
Links
Metadata
Release files for safer-yaml 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| safer_yaml-0.1.1.tar.gz | 6.4 kB | Without provenance | Details for safer_yaml-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| safer_yaml-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for safer_yaml-0.1.1-py3-none-any.whl |
Total release size: 11.0 kB
Release files / safer_yaml-0.1.1.tar.gz
| Download URL | safer_yaml-0.1.1.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bd930bf908a6ed65477427ea49a2598654ed57044e78961675ef9302481f8222
|
|
BLAKE2b-256 checksum How to use checksums |
2ec8b67cc437c270f0ea44bb5e6564d071028f594dbb2e0748ede34a061cd577
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|
Release files / safer_yaml-0.1.1-py3-none-any.whl
| Download URL | safer_yaml-0.1.1-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6e7934d1204283695ca1e74f9d59c52898648792af49cebe609cd7e6c3bf28d7
|
|
BLAKE2b-256 checksum How to use checksums |
cb4a0f7e1f6f199894be48962319288948c15be5630e2adc1bb419946b66d95d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|
Security for safer-yaml 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.