diff --git a/CHANGELOG.md b/CHANGELOG.md index 29373b09..5be20aee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Speed up parsing of comments: the comment text is scanned in one pass up to the line break or the first invalid control character, instead of one character at a time. ([#723](https://github.com/python-poetry/tomlkit/pull/723)) +### Fixed + +- Fix deleting one of several nested dotted keys inside an inline table writing invalid TOML: deleting `c` from `x = {a.b.c = 1, a.b.d = 2}` gave `x = {a.b = {}, a.b.d = 2}`, which defines `a.b` twice. The emptied part is now removed from the document, as it already was for a single-level dotted key. ([#732](https://github.com/python-poetry/tomlkit/pull/732)) + ## [0.15.3] - 2026-11-24 ### Changed diff --git a/tests/test_items.py b/tests/test_items.py index 4114be25..fee4b68e 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -1516,6 +1516,41 @@ def test_emptying_nested_dotted_key_inside_inline_table() -> None: assert parse(doc.as_string()) == doc +@pytest.mark.parametrize( + ("content", "path", "deleted", "expected"), + [ + ("a = {b.c.d = 1, b.c.e = 2}\n", ["b", "c"], ["d"], {"e": 2}), + ("a = {b.c.d = 1, b.c.e = 2}\n", ["b", "c"], ["e"], {"d": 1}), + ( + "a = {b.c.d = 1, b.c.e = 2, b.c.f = 3}\n", + ["b", "c"], + ["e"], + {"d": 1, "f": 3}, + ), + ("a = {b.c.d = 1, b.c.e = 2}\n", ["b", "c"], ["d", "e"], {}), + ("a = {b.c.d.e = 1, b.c.d.f = 2}\n", ["b", "c", "d"], ["e"], {"f": 2}), + ], +) +def test_deleting_one_of_several_nested_dotted_keys_inside_inline_table( + content: str, path: list[str], deleted: list[str], expected: dict[str, int] +) -> None: + doc = parse(content) + for key in deleted: + table = doc["a"] + for part in path: + table = table[part] + del table[key] + + # The emptied part must not render as ``b.c = {}`` next to the + # remaining ``b.c.x`` keys, which would define ``b.c`` twice. + reparsed = parse(doc.as_string()) + assert reparsed == doc + reparsed_table = reparsed["a"] + for key in path: + reparsed_table = reparsed_table[key] + assert reparsed_table == expected + + def test_appending_to_comma_first_array_does_not_double_separator() -> None: doc = parse( """\ diff --git a/tomlkit/container.py b/tomlkit/container.py index 924091a4..36720007 100644 --- a/tomlkit/container.py +++ b/tomlkit/container.py @@ -1156,6 +1156,10 @@ def __init__(self, container: Container, indices: tuple[int, ...]) -> None: self._internal_container = Container(True) self._tables: list[Table] = [] self._tables_map: dict[Key, list[int]] = {} + # Set when this proxy is looked up through another proxy. Its tables + # then sit both in that proxy's merged container and in the parent's + # real tables, so removing one must update both. + self._parent: OutOfOrderTableProxy | None = None for i in indices: _, _item = self._container._body[i] @@ -1236,7 +1240,10 @@ def __getitem__(self, key: Key | str) -> Any: if key not in self._internal_container: raise NonExistentKey(key) - return self._internal_container[key] + value = self._internal_container[key] + if isinstance(value, OutOfOrderTableProxy): + value._parent = self + return value def __setitem__(self, key: Key | str, value: Any) -> None: from .items import item as _item_fn @@ -1297,6 +1304,21 @@ def _remove_table(self, table: Table) -> None: if body_item[1] is table: self._container._remove_at(idx) break + if self._parent is not None: + # self._container is only the parent proxy's merged container, + # which is not rendered: remove the table from the parent's real + # table that holds it too. + self._parent._remove_child_table(table) + + def _remove_child_table(self, table: Table) -> None: + """Remove table from the part of this proxy that holds it""" + for part in self._tables: + for idx, body_item in enumerate(part.value._body): + if body_item[1] is table: + part.value._remove_at(idx) + if not part and len(self._tables) > 1: + self._remove_table(part) + return def __delitem__(self, key: Key | str) -> None: _key: Key = key if isinstance(key, Key) else SingleKey(key)