From 4ef7959765376ce51573b7f53b80d51121b3e744 Mon Sep 17 00:00:00 2001 From: zachyang374 <61482779+zachyang374@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:37:31 -0800 Subject: [PATCH 1/2] Fix invalid TOML after deleting a nested dotted key in an inline table Deleting one of several dotted keys that share a two-level prefix inside an inline table rendered the emptied part as `prefix = {}` next to the remaining keys: x = {a.b.c = 1, a.b.d = 2} -> x = {a.b = {}, a.b.d = 2} which defines `a.b` twice. The proxy for `x["a"]["b"]` is looked up through the `x["a"]` proxy, so `_remove_table()` removed the emptied part only from that proxy's merged container, not from the real `a` part that is rendered. Proxies now keep a reference to the proxy they were looked up through and also remove the table from its real part. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 ++++ tests/test_items.py | 35 +++++++++++++++++++++++++++++++++++ tomlkit/container.py | 24 +++++++++++++++++++++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29373b09..2bceb4c0 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. ([#PR_NUMBER](https://github.com/python-poetry/tomlkit/pull/PR_NUMBER)) + ## [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) From e5ba70e7eb164ca6081823f266c8faf1bc3c62df Mon Sep 17 00:00:00 2001 From: zachyang374 <61482779+zachyang374@users.noreply.github.com> Date: Thu, 3 Dec 2026 23:00:49 -0800 Subject: [PATCH 2/2] Add PR number to changelog entry Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bceb4c0..5be20aee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ ### 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. ([#PR_NUMBER](https://github.com/python-poetry/tomlkit/pull/PR_NUMBER)) +- 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