Skip to content

Fix out-of-range negative keypath index assignment - #675

Open
rahul05ranjan wants to merge 1 commit into
fabiocaccamo:mainfrom
rahul05ranjan:fix/issue-674
Open

rahul05ranjan wants to merge 1 commit into
fabiocaccamo:mainfrom
rahul05ranjan:fix/issue-674

Conversation

@rahul05ranjan

Copy link
Copy Markdown

Describe your changes

_set_item_value in benedict/dicts/keylist/keylist_util.py fell back to item.insert(index, value) when item[index] = value raised IndexError. list.insert clamps out-of-range positions instead of raising, so setting an out-of-range negative keypath index clamped the value to position 0 without ever extending the list: the written value could not be read back through the same keypath, and existing list values were shifted.

>>> d = benedict({"a": [1]})
>>> d["a[-3]"] = 9
>>> d
{'a': [9, 1]}
>>> d["a[-3]"]
KeyError: "Invalid keys: ['a', -3]"

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:

>>> d = benedict({"a": [1]})
>>> d["a[-3]"] = 9
>>> d
{'a': [9, None, 1]}
>>> d["a[-3]"]
9

In-range negative indexes (e.g. d["a[-1]"] = value) are untouched: they never reach the IndexError path.

One existing expectation in tests/dicts/keylist/test_keylist_util.py::test_set_item_with_indexes is updated: for set_item(d, ["a", "b", "e", -11], 10) it asserted the clamped [10, 4, 5, 6, None, None, 9], whose 10 was unreachable (list length 7, index -11). With this change the list is extended to length 11 and ["a", "b", "e", -11] reads 10 back.

Related issue

Fixes #674

Validation

  • New regression tests (fail on main@e910141, pass with this change):
    • tests/dicts/keypath/test_keypath_dict_list_indexes.py: test_setitem_with_out_of_range_negative_index and ..._and_missing_list (plus a positive-index control that already passes on main).
    • tests/dicts/keylist/test_keylist_util.py: test_set_item_with_out_of_range_negative_index asserts the write/read invariant via keylist_util.get_item.
  • python -m pytest tests/dicts/keylist tests/dicts/keypath tests/core -q: 222 passed, 22 subtests passed (only pre-existing test_standardize env failure remains, identical on main without this change).
  • Full suite failure list diffed against unpatched main under the same environment: byte-identical 130 pre-existing failures (optional-parser/environment tests), no new failures introduced.
  • ruff check and ruff format --check pass on the changed files.

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 no errors (relevant suites fully green; pre-existing environment-caused failures disclosed above are identical on main).

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>
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.

Setting an out-of-range negative list index corrupts the list and the value cannot be read back

1 participant