Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion minimal_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def safe_load(stream):
line = f"{indent}{key}: {val}"
elif alias_def:
indent, key, anc = alias_def.groups()
val = anchors.get(anc)
if anc not in anchors:
raise ValueError(f'Alias "{anc}" not defined')
val = anchors[anc]
val_repr = repr(val) if isinstance(val, str) else str(val)
line = f"{indent}{key}: {val_repr}"
processed.append(line)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_minimal_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import sys
import pytest

sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from minimal_yaml import safe_load


def test_alias_resolution():
text = """
key1: &val 123
key2: *val
"""
data = safe_load(text)
assert data["key1"] == 123
assert data["key2"] == 123


def test_unknown_alias_error():
text = "key: *missing"
with pytest.raises(ValueError):
safe_load(text)