Skip to content

Commit e3b7332

Browse files
authored
fix: ensure unique table indices when adding items to out-of-order tables (#420)
Fix #383 Signed-off-by: Frost Ming <[email protected]>
1 parent 14607a5 commit e3b7332

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
### Fixed
1010

11+
- Fix non-existing key error when deleting an item from an out-of-order table. ([#383](https://github.com/python-poetry/tomlkit/issues/383))
1112
- Fix repeated whitespace when removing an array item. ([#405](https://github.com/python-poetry/tomlkit/issues/405))
1213
- Fix invalid serialization after removing array item if the comma is on its own line. ([#408](https://github.com/python-poetry/tomlkit/issues/408))
1314
- Fix serialization of a nested dotted key table. ([#411](https://github.com/python-poetry/tomlkit/issues/411))

tests/test_toml_document.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,3 +1231,27 @@ def test_overwriting_out_of_order_table():
12311231
bar = {open = true}
12321232
"""
12331233
)
1234+
1235+
1236+
def test_delete_key_from_out_of_order_table():
1237+
content = """\
1238+
[foo.bar.baz]
1239+
a = 1
1240+
[foo.bar]
1241+
b = 2
1242+
[other]
1243+
c = 3
1244+
[foo.tee]
1245+
d = 4
1246+
"""
1247+
doc = parse(content)
1248+
del doc["foo"]["bar"]
1249+
assert (
1250+
doc.as_string()
1251+
== """\
1252+
[other]
1253+
c = 3
1254+
[foo.tee]
1255+
d = 4
1256+
"""
1257+
)

tomlkit/container.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,9 @@ def __init__(self, container: Container, indices: tuple[int, ...]) -> None:
814814
table_idx = len(self._tables) - 1
815815
for k, v in item.value.body:
816816
self._internal_container._raw_append(k, v)
817-
self._tables_map.setdefault(k, []).append(table_idx)
817+
indices = self._tables_map.setdefault(k, [])
818+
if table_idx not in indices:
819+
indices.append(table_idx)
818820
if k is not None:
819821
dict.__setitem__(self, k.key, v)
820822

0 commit comments

Comments
 (0)