Repository navigation
Fix out-of-range negative keypath index assignment - #675
Open
rahul05ranjan wants to merge 1 commit into
Open
rahul05ranjan wants to merge 1 commit into
rahul05ranjan wants to merge 1 commit into
Conversation
Extend the list on the left when setting an out-of-range negative index so the written value is readable back through the same keypath, keeping existing values at their positions. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
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.
Describe your changes
_set_item_valueinbenedict/dicts/keylist/keylist_util.pyfell back toitem.insert(index, value)whenitem[index] = valueraisedIndexError.list.insertclamps out-of-range positions instead of raising, so setting an out-of-range negative keypath index clamped the value to position0without ever extending the list: the written value could not be read back through the same keypath, and existing list values were shifted.The fix extends the list on the left so that the negative index becomes valid, keeping existing values at their positions — mirroring what the positive-index path already does on the right:
In-range negative indexes (e.g.
d["a[-1]"] = value) are untouched: they never reach theIndexErrorpath.One existing expectation in
tests/dicts/keylist/test_keylist_util.py::test_set_item_with_indexesis updated: forset_item(d, ["a", "b", "e", -11], 10)it asserted the clamped[10, 4, 5, 6, None, None, 9], whose10was unreachable (list length 7, index -11). With this change the list is extended to length 11 and["a", "b", "e", -11]reads10back.Related issue
Fixes #674
Validation
main@e910141, pass with this change):tests/dicts/keypath/test_keypath_dict_list_indexes.py:test_setitem_with_out_of_range_negative_indexand..._and_missing_list(plus a positive-index control that already passes onmain).tests/dicts/keylist/test_keylist_util.py:test_set_item_with_out_of_range_negative_indexasserts the write/read invariant viakeylist_util.get_item.python -m pytest tests/dicts/keylist tests/dicts/keypath tests/core -q: 222 passed, 22 subtests passed (only pre-existingtest_standardizeenv failure remains, identical onmainwithout this change).mainunder the same environment: byte-identical 130 pre-existing failures (optional-parser/environment tests), no new failures introduced.ruff checkandruff format --checkpass on the changed files.Checklist before requesting a review
main).