Skip to content

fix(python): preserve YAML sequence merge precedence on PyYAML 6.0.4 - #782

Merged
christianeu-db merged 1 commit into
apache:mainfrom
jbonofre:fix/pyyaml-sequence-merge-precedence
Nov 21, 2026
Merged

christianeu-db merged 1 commit into
apache:mainfrom
jbonofre:fix/pyyaml-sequence-merge-precedence

Conversation

@jbonofre

Copy link
Copy Markdown
Member

Summary

PyYAML 6.0.4 removed submerge.reverse() in SafeConstructor.flatten_mapping when expanding sequence merges (<<: [first, second]) and deduplicated keys by id(pair[0].value) from the left. Because construct_mapping assigns pairs in forward order (last pair wins), later entries in a sequence merge overwrite earlier ones under PyYAML 6.0.4, breaking YAML 1.1 sequence merge precedence and causing UniqueKeyLoaderTest.test_preserves_merge_precedence and test_loading.py to fail in CI.

  • Override flatten_mapping in UniqueKeyLoader (python/src/ossie/_loading.py) to reverse sequence merge sources so earlier elements override later ones across both PyYAML 6.0.3 and 6.0.4+, while deduplicating identical key nodes from the right so repeated anchor references cannot expand node.value exponentially.
  • Update python/tests/test_loading.py to assert expected merged dictionaries directly instead of comparing sequence-merge results against yaml.safe_load.

Testing

  • uv run --with "pyyaml==6.0.3" validation/test_validate.py (59 passed)
  • uv run --with "pyyaml==6.0.4" validation/test_validate.py (59 passed)
  • uv run --with pytest --with "pyyaml==6.0.3" --with jsonschema --with ./python -m pytest python/tests/ (295 passed)
  • uv run --with pytest --with "pyyaml==6.0.4" --with jsonschema --with ./python -m pytest python/tests/ (295 passed)

PyYAML 6.0.4 removed submerge.reverse() in SafeConstructor.flatten_mapping
when expanding sequence merges (<<: [first, second]) and deduplicated keys
by id(pair[0].value) from the left, causing later sequence entries to
override earlier ones in construct_mapping.

Override flatten_mapping in UniqueKeyLoader to reverse sequence merge
sources (preserving YAML 1.1 sequence merge precedence across PyYAML 6.0.3
and 6.0.4+) while deduplicating identical key nodes from the right so
repeated anchor references cannot expand node.value exponentially.

@kayemkim kayemkim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Against main's loader on PyYAML 6.0.3, this one gives the same result or the same error for every one of about 19,000 generated merge documents (aliases, nested and repeated merges, sequence merges, = and literal << keys, duplicate keys), on 6.0.3 and on 6.0.4. The fan-out document from PyYAML's own test loads in milliseconds on both versions; with 6.0.3's own flatten it takes about three seconds at ten levels. LGTM.

@christianeu-db
christianeu-db merged commit 83d39d6 into apache:main Nov 21, 2026
13 checks 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.

3 participants