Skip to content

Fix unique handling of literal index-like keys - #666

Open
tanveerchana wants to merge 1 commit into
fabiocaccamo:mainfrom
tanveerchana:fix-unique-literal-keys
Open

tanveerchana wants to merge 1 commit into
fabiocaccamo:mainfrom
tanveerchana:fix-unique-literal-keys

Conversation

@tanveerchana

@tanveerchana tanveerchana commented Nov 3, 2026 •

Copy link
Copy Markdown

Describe your changes

unique() currently interprets stored dictionary keys as paths when reading values and removing duplicates. A literal key such as rows[0] can therefore cause an unrelated list element to be deleted:

from benedict import benedict

data = {"first": 1, "rows": [1, 2], "rows[0]": 1}
benedict(data).unique()
# Before: {"first": 1, "rows": [2], "rows[0]": 1}
# After:  {"first": 1, "rows": [1, 2]}

Iterate over a snapshot of stored items and remove duplicate literal keys through BaseDict.pop, preserving wrapped-dictionary updates and frozen-input checks. Keep the generic MutableMapping fallback. Regression tests cover wrapped/unwrapped dictionaries, a distinct literal value, a missing path parent with unhashable duplicates, and frozen input.

Related issue

No matching existing issue or PR found. Reproduced during code inspection on main e910141, python-benedict 0.40.1, CPython 3.12.14/Linux. This is independently reproduced behavior, not a claimed production incident.

Validation

  • Before the fix: the three behavioral regression methods fail (including both wrapped/unwrapped subcases); the frozen-input control passes.
  • python -m pytest tests/core/test_unique.py tests/dicts/test_benedict.py -q: 96 passed, 7 subtests passed.
  • pre-commit run --files benedict/core/unique.py tests/dicts/test_benedict.py: all applicable hooks passed.
  • ruff check . and ruff format --check .: passed (197 files formatted).
  • mypy benedict: passed, 65 source files.
  • coverage run --source=benedict -m unittest: 994 tests run; 8 errors from existing remote fixture downloads blocked by the environment proxy (HTTP CONNECT 403). These affect plist, query-string, toml, xls/xlsx/xlsm, xml and yaml URL tests. No full-suite success is claimed. AWS credentials were left unset so existing live S3 tests did not perform transfers.
  • python -m pytest -q -k 'not url': 947 passed, 47 deselected, 185 subtests passed (URL tests excluded).
  • coverage report --include=benedict/core/unique.py: 100% statement coverage.

Checklist before requesting a review

  • I have performed a self-review of my code.
  • I have added tests for the proposed changes.
  • I have run the tests and there are not errors. (Relevant tests pass; the full-suite network errors are disclosed above.)

Compare stored item values and remove duplicate raw keys through BaseDict
so unique cannot pop an unrelated nested list element. Preserve frozen
and wrapped dictionary behavior and add regression coverage.

AI assistance: implemented and tested with OpenAI Codex.
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