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
30 changes: 30 additions & 0 deletions tests/test_toml_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,36 @@ def test_delete_key_from_out_of_order_table() -> None:
)


def test_delete_keys_through_the_same_out_of_order_table() -> None:
content = """\
[tool.a]
x = 1
[project]
name = "p"
[tool.b]
y = 2
[other]
[tool.c]
z = 3
"""
doc = parse(content)
tool = doc["tool"]
del tool["a"]
del tool["b"]
assert (
doc.as_string()
== """\
[project]
name = "p"
[other]
[tool.c]
z = 3
"""
)
tool["c"] = {"w": 4}
assert doc["tool"] == {"c": {"w": 4}}


def test_parse_aot_without_ending_newline() -> None:
content = '''\
[[products]]
Expand Down
8 changes: 6 additions & 2 deletions tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,11 @@ def _is_table_or_aot(it: Any) -> bool:

def _remove_table(self, table: Table) -> None:
"""Remove table from the parent container"""
self._tables.remove(table)
pos = next(i for i, t in enumerate(self._tables) if t is table)
del self._tables[pos]
# Later tables shift down by one; keep the key -> table index map valid.
for indices in self._tables_map.values():
indices[:] = [i - 1 if i > pos else i for i in indices if i != pos]
for idx, body_item in enumerate(self._container._body):
if body_item[1] is table:
self._container._remove_at(idx)
Expand All @@ -1238,7 +1242,7 @@ def __delitem__(self, key: Key | str) -> None:
if _key not in self._tables_map:
raise NonExistentKey(key)

for i in reversed(self._tables_map[_key]):
for i in reversed(list(self._tables_map[_key])):
table = self._tables[i]
del table[key]
if not table and len(self._tables) > 1:
Expand Down
Loading