Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,26 @@ def test_array_behaves_like_a_list() -> None:
)


def test_array_slice_deletion_keeps_render_in_sync() -> None:
# Regression: deleting a slice must keep as_string() consistent with the
# list contents. Slices with stop == 0 or negative bounds previously
# corrupted the rendered array (e.g. `del a[:0]` wiped every element).
a = item([1, 2, 3])
del a[:0] # deletes nothing
assert a == [1, 2, 3]
assert a.as_string() == "[1, 2, 3]"

a = item([1, 2, 3])
del a[-1:]
assert a == [1, 2]
assert a.as_string() == "[1, 2]"

a = item([1, 2, 3])
del a[:-1]
assert a == [3]
assert a.as_string() == "[3]"


def test_array_multiline() -> None:
t = item([1, 2, 3, 4, 5, 6, 7, 8])
t.multiline(True)
Expand Down
4 changes: 1 addition & 3 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -1657,9 +1657,7 @@ def __delitem__(self, key: int | slice) -> None: # type: ignore[override]
list.__delitem__(self, key)

if isinstance(key, slice):
indices_to_remove = list(
range(key.start or 0, key.stop or length, key.step or 1)
)
indices_to_remove = list(range(*key.indices(length)))
else:
indices_to_remove = [length + key if key < 0 else key]
for i in sorted(indices_to_remove, reverse=True):
Expand Down