Skip to content

🐛 Catch Python-equal numeric duplicate keys under OPT_DUPLICATE_KEYS_ERROR#182

Merged
frenck merged 1 commit into
mainfrom
frenck/dup-keys-python-equal
Jul 3, 2026
Merged

🐛 Catch Python-equal numeric duplicate keys under OPT_DUPLICATE_KEYS_ERROR#182
frenck merged 1 commit into
mainfrom
frenck/dup-keys-python-equal

Conversation

@frenck

@frenck frenck commented Jul 3, 2026

Copy link
Copy Markdown
Owner

Breaking change

None by default (last-wins is unchanged). With OPT_DUPLICATE_KEYS_ERROR set, a mapping that repeats a key under two Python-equal spellings (1 and 1.0, True and 1, 0 and False) now raises YAMLRocksDuplicateKeyError instead of silently keeping the last value, matching ruamel.

Proposed change

Keys distinct in YAML but equal as Python dict keys collapse to one entry once the mapping becomes a dict, so one silently overwrote the other even with the strict option set. Detection used a type-distinct signature (merge's key_sig), which kept 1 and 1.0 apart.

This adds a Python-equality signature that collapses a bool/int/bigint/integral-float to the same integer signature, used in both duplicate-key paths (the fast decoder and the round-trip composer). Numerically distinct keys (1 vs 2, 1 vs 1.5) and cross-type keys (1 vs "1") are still allowed. The now-unused key_sig import is dropped.

Type of change

  • Dependency or tooling upgrade
  • Bugfix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Deprecation (replaces or removes a feature, with a migration path)
  • Breaking change (a fix or feature that changes existing behavior)
  • Code quality, refactor, or test-only change
  • Documentation only

Additional information

  • This PR fixes or closes issue: None
  • This PR is related to: None
  • Link to a separate documentation pull request: None

Checklist

  • I have read the AI Policy, and this pull request was not created by an autonomous agent.
  • I fully understand the code in this pull request and can explain every line, including any AI-assisted changes.
  • The change is covered by tests, and uv run pytest passes locally. A pull request cannot be merged unless CI is green.
  • uv run ruff check . and uv run ruff format --check . pass.
  • cargo fmt --check and cargo clippy --all-targets -- -D warnings pass.
  • Round-trip fidelity is preserved: an unmodified document still re-emits byte-for-byte.
  • No commented-out or dead code is left in the pull request.

Copilot AI review requested due to automatic review settings July 3, 2026 00:12
@frenck frenck added the bugfix Inconsistencies or issues which will cause a problem for users or implementers. label Jul 3, 2026
@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@frenck, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 58 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 152a07e2-35a0-4802-af1e-0e61aa27942f

📥 Commits

Reviewing files that changed from the base of the PR and between 19c5a83 and f33fc0c.

📒 Files selected for processing (3)
  • src/decode/mod.rs
  • src/roundtrip/composer.rs
  • tests/features/test_options.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codspeed-hq

codspeed-hq Bot commented Jul 3, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing frenck/dup-keys-python-equal (f33fc0c) with main (19c5a83)

Open in CodSpeed

@codecov-commenter

codecov-commenter commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 53.65854% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.29%. Comparing base (19c5a83) to head (f33fc0c).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/decode/mod.rs 46.87% 17 Missing ⚠️
src/roundtrip/composer.rs 77.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #182      +/-   ##
==========================================
- Coverage   92.42%   92.29%   -0.13%     
==========================================
  Files          40       40              
  Lines       11242    11278      +36     
==========================================
+ Hits        10390    10409      +19     
- Misses        852      869      +17     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes duplicate-key detection under the opt-in OPT_DUPLICATE_KEYS_ERROR (and OPT_DUPLICATE_KEYS_WARN) mode so that keys which are distinct in YAML but equal as Python dict keys (1/1.0/True, 0/False, integral floats vs ints) are now flagged instead of silently collapsing to the last value once the mapping becomes a dict. Previously detection used merge's type-distinct key_sig, which kept these apart. Default last-wins behavior is unchanged. The change is applied consistently across the fast decoder and the round-trip/annotated AST composer, matching ruamel's behavior.

Changes:

  • Add a Python-equality key signature that collapses bool/int/bigint/integral-float to a shared n: signature in the fast decode path (python_dup_key_sig + float_dup_sig) and drop the now-unused key_sig import.
  • Mirror the same numeric-collapsing logic in the round-trip/annotated AST composer's key_signature, preserving the s:<< merge-key exemption.
  • Add tests covering Python-equal numeric duplicate keys (fast + annotated paths) and confirming numerically distinct keys remain allowed.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/decode/mod.rs Adds python_dup_key_sig/float_dup_sig and switches check_duplicate_key to use it; removes the unused key_sig import (verified no remaining references).
src/roundtrip/composer.rs Updates key_signature numeric branches to collapse bool/int/bigint/integral-float per Python dict semantics; doc comment updated accordingly.
tests/features/test_options.py Adds parametrized tests for Python-equal numeric duplicate keys and for numerically distinct keys remaining allowed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/features/test_options.py Outdated
…ERROR

Keys that are distinct in YAML but equal as Python dict keys (1 and 1.0, True
and 1, 0 and False) collapse to one entry once the mapping becomes a dict, so
one silently overwrote the other even with the strict duplicate-key option set.
The detection keyed off a type-distinct signature (merge's key_sig), which kept
1 and 1.0 apart. ruamel raises DuplicateKeyError here.

Add a Python-equality signature that collapses a bool/int/bigint/integral-float
to the same integer signature, and use it in both duplicate-key paths (the fast
decoder and the round-trip composer). Numerically distinct keys (1 vs 2, 1 vs
1.5) and cross-type keys (1 vs "1") are still allowed.
@frenck frenck force-pushed the frenck/dup-keys-python-equal branch from d218947 to f33fc0c Compare July 3, 2026 07:03
@frenck frenck merged commit 54d55e7 into main Jul 3, 2026
73 checks passed
@frenck frenck deleted the frenck/dup-keys-python-equal branch July 3, 2026 07:45
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants