Skip to content

Support YAML rule files alongside JSON#111

Merged
matthewhorridge merged 1 commit into
mainfrom
feat/yaml-rules
Jun 4, 2026
Merged

Support YAML rule files alongside JSON#111
matthewhorridge merged 1 commit into
mainfrom
feat/yaml-rules

Conversation

@matthewhorridge

Copy link
Copy Markdown
Contributor

Summary

Adds YAML as a supported serialization for rule files, alongside the existing JSON. RuleSet.save/load choose the format by file extension: .yaml/.yml read and write YAML, anything else JSON. Both encode the same flat array of rule dicts, so a rule set round-trips through either form, and the CLI's --rules accepts a YAML path automatically (it loads via RuleSet.load).

Why

YAML is more readable for hand-authoring and reviewing rules, and it preserves integer mapping keys natively — unlike a JSON object, whose keys are always strings. (An int-keyed enum_to_enum map, e.g. a one-hot index → label, therefore matches integer inputs correctly after a YAML round-trip.)

Details

  • rule_registry.py: _is_yaml / _blank_line_between_rules helpers; save/load branch on extension.
  • YAML is emitted with default_flow_style=None — scalar-only collections (a sources list, a {from, to} mapping entry) render inline while the surrounding structure stays block — and a blank line separates top-level rules, so multi-rule files stay readable.
  • requirements.txt: add pyyaml.
  • cli.py: --rules help text notes JSON or YAML.

Testing

tests/test_yaml_rules.py covers: round-trip including int keys, JSON/YAML output equivalence on harmonized data, the hybrid-flow layout, extension detection (.yml/.json/unknown→JSON), and a CLI run with rules.yaml. Full suite: 182 passed.

🤖 Generated with Claude Code

RuleSet.save/load now choose the format by file extension: .yaml/.yml read and
write YAML, anything else JSON. Both encode the same flat array of rule dicts,
so a rule set round-trips through either form. The CLI's --rules accepts a YAML
path automatically (it loads via RuleSet.load).

YAML is emitted with default_flow_style=None — scalar-only collections (a
sources list, a {from, to} mapping entry) render inline while the surrounding
structure stays block — and a blank line separates top-level rules, so the file
stays readable. YAML preserves integer mapping keys natively (unlike a JSON
object), so int-keyed enum maps match integer inputs after a round-trip.

- rule_registry.py: _is_yaml / _blank_line_between_rules helpers; save/load
  branch on extension.
- requirements.txt: add pyyaml.
- cli.py: --rules help text notes JSON or YAML.
- tests/test_yaml_rules.py: round-trip incl. int keys, JSON/YAML output
  equivalence, hybrid-flow layout, extension detection, CLI with rules.yaml.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@matthewhorridge matthewhorridge merged commit 330c732 into main Jun 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant