YAML 1.2 Support for PyYAML
This module provides YAML 1.2 Core Schema support on top of PyYAML. It acts as an extension, depending on and inheriting from PyYAML, rather than being a fork. It enables all YAML 1.2 Core Schema tags for PyYAML's BaseLoader. Developed as an alternative while native YAML 1.2 support was pending in PyYAML, it is actively maintained. The current version is 0.0.4, released in October 2024, with a focused release cadence for improvements and fixes.
Common errors
-
yaml.YAMLError: duplicated key: <key_name>
cause `yamlcore` (from v0.0.3) strictly enforces the YAML 1.2 specification, which prohibits duplicate keys within a single mapping. Earlier versions of PyYAML might have silently overwritten duplicate keys.fixReview your YAML file to identify and remove any duplicate keys within mappings. Each key in a YAML dictionary must be unique. Use YAML validators or linters to pinpoint exact locations. -
yaml.YAMLError: mapping values are not allowed in this context
cause This error often indicates incorrect indentation or the use of tabs instead of spaces in your YAML file. YAML is highly sensitive to whitespace for structural definition, and tabs are universally forbidden by the spec.fixEnsure all indentation uses spaces consistently (usually 2 or 4 spaces per level) and that no tabs are present in your YAML file. Many text editors have features to convert tabs to spaces or highlight mixed indentation. -
TypeError: load() missing 1 required positional argument: 'Loader'
cause Starting with PyYAML 5.1 (and therefore affecting `yamlcore` usage indirectly), calling `yaml.load()` without explicitly providing a `Loader` argument is deprecated and will eventually become an error, to enforce safer loading practices.fixAlways specify a `Loader` when calling `yaml.load()`. For `yamlcore`'s YAML 1.2 capabilities, use `yaml.load(your_data, Loader=CoreLoader)` (or `CCoreLoader`). For general safe PyYAML loading, use `yaml.load(your_data, Loader=yaml.SafeLoader)`.
Warnings
- breaking From `yamlcore` v0.0.3 onwards, duplicate keys in YAML mappings are explicitly forbidden and will raise an error during loading. This is a significant change from standard PyYAML's historical behavior, which silently overwrites earlier duplicate keys, and aligns with the YAML specification.
- gotcha Using `yaml.load()` (from PyYAML, which `yamlcore` builds upon) without explicitly specifying a Loader (e.g., `Loader=CoreLoader` or `Loader=yaml.SafeLoader`) can be a severe security risk if processing untrusted input. The default `UnsafeLoader` allows arbitrary code execution.
- gotcha `yamlcore` currently only supports enabling YAML 1.2 Core Schema tags and does not yet support other advanced YAML features like the `<<` merge key. If your YAML relies on such features, `yamlcore` might not parse it as expected.
- gotcha YAML 1.1 parsers (like older PyYAML versions) treat certain literals like `yes`, `no`, `on`, `off` as booleans and numbers like `10:20` (base-60) as integers, which were changed in YAML 1.2 to be typically parsed as strings or different types. While `yamlcore` aims for YAML 1.2 compliance, be aware of this historical context when migrating or dealing with mixed YAML versions.
Install
-
pip install yamlcore
Imports
- CoreLoader
from yamlcore import CoreLoader
- CoreDumper
from yamlcore import CoreDumper
- CCoreLoader
from yamlcore import CCoreLoader
- CCoreDumper
from yamlcore import CCoreDumper
Quickstart
import yaml
from yamlcore import CoreLoader, CoreDumper
yaml_string = """
--- 1.1:
- yes
- no
- 1__0
- 10:20
- +0b100
- 0x4_2
core:
- true
- 0o10
- 0x42
- ~
- .inf
"""
# Load using CoreLoader for YAML 1.2 compliance
data = yaml.load(yaml_string, Loader=CoreLoader)
print("Loaded data (Python object):")
print(data)
# Dump using CoreDumper for YAML 1.2 compliance
output_yaml = yaml.dump(data, Dumper=CoreDumper)
print("\nDumped data (YAML string):")
print(output_yaml)
# Example with a simple dictionary
simple_data = {'name': 'Alice', 'age': 30, 'is_active': True}
simple_yaml = yaml.dump(simple_data, Dumper=CoreDumper, default_flow_style=False)
print("\nSimple data dumped to YAML:")
print(simple_yaml)