Fix Array.__delitem__ corrupting rendered output on slice deletion - #599
Open
Fries-tempura wants to merge 1 commit into
Open
Fries-tempura wants to merge 1 commit into
Fries-tempura wants to merge 1 commit into
Conversation
Jalst
approved these changes
Sep 17, 2026
Jalst
left a comment
There was a problem hiding this comment.
LGTM! Verified the fix and tested for regressions.
Analysis & Root Cause
The previous implementation:
range(key.start or 0, key.stop or length, key.step or 1)suffered from two issues:
- For
stop == 0(e.g.del a[:0]),0 or lengthevaluated tolength, mistakenly wiping out all rendered items fromself._value. - Negative indices were passed directly into
self._index_map, which only indexes non-negative positions, causing key misses and state desynchronization between the underlyinglistand renderedself._value.
Using standard-library key.indices(length) correctly resolves None, negative offsets, and bounds according to Python list slice semantics. Furthermore, because indices_to_remove is sorted in reverse order (reverse=True) and self._index_map is strictly monotonic, deleting higher indices first guarantees that lower _value offsets remain valid throughout the deletion loop before self._reindex() is invoked.
Verification
- Ran full test suite: 1,053 passed.
- Tested 500 randomized slice permutations (varying negative/positive bounds, steps > 1, negative steps, empty slices, and slices past array boundaries) across single-line and multiline arrays with inline comments. In all cases,
a.as_string()round-trips and agrees withlist(a). - Clean diff with zero side-effects on existing formatting logic.
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
Array.__delitem__keeps the in-memory list and the rendered value groups in sync, but for slice deletions it recomputes which rendered positions to drop with:This mishandles ordinary slices:
stop == 0(a[:0]):0 or lengthevaluates tolength, so a no-op deletion removes every rendered element.a[-1:]→range(-1, length), anda[:-1]→range(0, -1)(empty, removes nothing).list.__delitem__handles the slice correctly, so the list contents stay right, butas_string()/dumps()silently disagree with them:list(arr)arr.as_string()del arr[:0][1, 2, 3][][1, 2, 3]del arr[-1:][1, 2][][1, 2]del arr[:-1][3][1, 2, 3][3]Fix: use
slice.indices(length), which resolvesNone, negative, and out-of-range bounds exactly the waylist.__delitem__does, so the removed render positions always match the list.Tests: added
test_array_slice_deletion_keeps_render_in_synccoveringa[:0],a[-1:], anda[:-1]. Fulltests/test_items.pypasses (87 passed).Agent Drafting Metadata
main. The one-line fix and the regression test were drafted with the agent and verified locally — the repro fails before the change andtests/test_items.pypasses (87 passed) after. Reviewed by the submitter before opening.