Repository navigation
Fix multi-character separators mangling the first path key - #218
Open
TrueFurina wants to merge 1 commit into
Open
TrueFurina wants to merge 1 commit into
TrueFurina wants to merge 1 commit into
Conversation
`_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
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
_split_pathstripped a leading separator withpath.lstrip(separator).str.lstriptreats its argument as a set of characters, not a string, so a multi-character separator (e.g."ab") would strip any leadinga/bcharacters from the first key. This silently selected or created the wrong key.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_pathdirect checks plusget/newintegration, and a single-character-separator back-compat check). The full suite (83 tests) passes.