From ebec0385259539c015a4d25828031ca67d83b5cf Mon Sep 17 00:00:00 2001 From: jju1s <123896289+cig13zs@users.noreply.github.com> Date: Fri, 16 Oct 2026 00:57:57 +0800 Subject: [PATCH 1/2] Keep an emptied dotted key inside an inline table Deleting the last key under a dotted prefix in an inline table left an empty slot between two commas: >>> doc = parse('t = {a = "", b.c = 1, d = 1}\n') >>> del doc["t"]["b"]["c"] >>> doc.as_string() 't = {a = "", , d = 1}\n' _render_dotted renders one "prefix.child = value" pair per child, so an emptied table produced no pairs at all while the separators around it were still emitted. Render it as "b = {}" instead, which keeps the empty table the document still holds and gives valid TOML wherever the key sits in the inline table. --- tests/test_items.py | 27 +++++++++++++++++++++++++++ tomlkit/items.py | 3 +++ 2 files changed, 30 insertions(+) diff --git a/tests/test_items.py b/tests/test_items.py index bbe1b2ed..ff753695 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -1258,6 +1258,33 @@ def test_adding_to_nested_dotted_key_inside_inline_table() -> None: assert parse(rendered).as_string() == rendered +@pytest.mark.parametrize( + ("content", "expected"), + [ + ("a = {x = 1, b.c = 1, y = 2}\n", "a = {x = 1, b = {}, y = 2}\n"), + ("a = {x = 1, b.c = 1}\n", "a = {x = 1, b = {}}\n"), + ("a = {b.c = 1, y = 2}\n", "a = {b = {}, y = 2}\n"), + ("a = {b.c = 1}\n", "a = {b = {}}\n"), + ], +) +def test_emptying_dotted_key_inside_inline_table(content: str, expected: str) -> None: + doc = parse(content) + del doc["a"]["b"]["c"] + + # With nothing left under the ``b.`` prefix, the table must still render + # as a key, not as an empty slot between two commas. + assert doc.as_string() == expected + assert parse(expected) == doc + + +def test_emptying_nested_dotted_key_inside_inline_table() -> None: + doc = parse("a = {x = 1, b.c.d = 1, y = 2}\n") + del doc["a"]["b"]["c"]["d"] + + assert doc.as_string() == "a = {x = 1, b.c = {}, y = 2}\n" + assert parse(doc.as_string()) == doc + + def test_appending_to_comma_first_array_does_not_double_separator() -> None: doc = parse( """\ diff --git a/tomlkit/items.py b/tomlkit/items.py index 26b01097..379630e8 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -2224,6 +2224,9 @@ def _render_dotted(self, key: Key, table: Table) -> list[str]: f"{prefix}{v.trivia.indent}{k.as_string()}{k.sep}{v.as_string()}" f"{v.trivia.comment_ws}{v.trivia.comment}{trail}" ) + if not parts: + # every key under the prefix was removed, keep it as an empty table + return [f"{key.as_string()} = {{}}"] return parts def __setitem__(self, key: Key | str, value: Any) -> None: From 6ada78613109e01d0141cb22d669ad2cd5a7c071 Mon Sep 17 00:00:00 2001 From: jju1s <123896289+cig13zs@users.noreply.github.com> Date: Fri, 16 Oct 2026 01:06:02 +0800 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0af52c54..f5ffa965 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixed +- Fix invalid output after deleting the last key under a dotted key inside an inline table; the emptied table now renders as `b = {}`. ([#664](https://github.com/python-poetry/tomlkit/pull/664)) - Fix valid TOML being rejected when an out-of-order table reopens an implicit super-table with a dotted key: `[a.b.c]` then `[a]` with `b.z = 1` extends the implicit `a.b` super-table rather than redefining it (stdlib `tomllib` accepts it), so it now parses and round-trips instead of raising `Redefinition of an existing table`. The concrete-table case (`[a.b]` then `[a]` `b.z = 1`) stays rejected. ([#565](https://github.com/python-poetry/tomlkit/pull/565)) - Fix appending a key to a parsed inline table that uses `", "` separators emitting a bare `","` for the new entry, leaving the table inconsistently spaced (e.g. `{x = 1, y = 2,z = 3}`). ([#595](https://github.com/python-poetry/tomlkit/pull/595))