Skip to content

Fix multi-character separators mangling the first path key - #218

Open
TrueFurina wants to merge 1 commit into
dpath-maintainers:masterfrom
TrueFurina:fix/multichar-separator-split
Open

TrueFurina wants to merge 1 commit into
dpath-maintainers:masterfrom
TrueFurina:fix/multichar-separator-split

Conversation

@TrueFurina

Copy link
Copy Markdown

Summary

_split_path stripped a leading separator with path.lstrip(separator). str.lstrip treats its argument as a set of characters, not a string, so a multi-character separator (e.g. "ab") would strip any leading a/b characters from the first key. This silently selected or created the wrong key.

import dpath
data = {'apple': {'key': 1}, 'pple': {'key': 2}}
dpath.get(data, 'appleabkey', separator='ab')            # -> 2  (should be 1)
created = {}
dpath.new(created, 'appleabkey', 1, separator='ab')     # -> {'pple': {'key': 1}}

Fix

Strip the separator as a whole string (repeatedly, so the existing behavior of removing leading separators is preserved for single-character separators too), then split on it. Single-character separators keep their exact behavior; multi-character ones are now correct.

Tests

Adds regression tests in tests/test_paths.py (_split_path direct checks plus get/new integration, and a single-character-separator back-compat check). The full suite (83 tests) passes.


`_split_path` used `path.lstrip(separator)` to strip a leading
separator. `str.lstrip` treats its argument as a *set of characters*,
not a string, so a multi-character separator (e.g. "ab") would strip
any leading 'a'/'b' characters from the first key. This silently
selected or created the wrong key:

    data = {'apple': {'key': 1}, 'pple': {'key': 2}}
    dpath.get(data, 'appleabkey', separator='ab')  # -> 2 (should be 1)
    dpath.new({}, 'appleabkey', 1, separator='ab')  # -> {'pple': ...}

Strip the separator as a whole string (repeatedly, to preserve the
existing behavior of removing leading separators for single-character
separators too), then split on it. Single-character separators keep
their behavior; multi-character ones are now correct.

Adds regression tests (test_paths.py).

Co-authored-by: LLM
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.

Multi-character separators can select or create a truncated key

1 participant