Support YAML rule files alongside JSON#111
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds YAML as a supported serialization for rule files, alongside the existing JSON.
RuleSet.save/loadchoose the format by file extension:.yaml/.ymlread 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--rulesaccepts a YAML path automatically (it loads viaRuleSet.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_enummap, 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_ruleshelpers;save/loadbranch on extension.default_flow_style=None— scalar-only collections (asourceslist, 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: addpyyaml.cli.py:--ruleshelp text notes JSON or YAML.Testing
tests/test_yaml_rules.pycovers: 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 withrules.yaml. Full suite: 182 passed.🤖 Generated with Claude Code