Fix invalid TOML after deleting a nested dotted key in an inline table
Summary
Deleting one of several dotted keys that share a two-level prefix inside an inline table wrote the emptied part as prefix = {} next to the keys that are left:
>>> doc = tomlkit.parse("x = {a.b.c = 1, a.b.d = 2}\n")
>>> del doc["x"]["a"]["b"]["c"]
>>> tomlkit.dumps(doc)
'x = {a.b = {}, a.b.d = 2}\n' # defines a.b twice; tomllib and tomlkit reject it
x["a"]["b"] is an OutOfOrderTableProxy over the two b parts, looked up through the x["a"] proxy. When __delitem__ empties a part, _remove_table() removes it from self._container, which here is the merged _internal_container of the x["a"] proxy rather than the document. The emptied b stays in the real a part, and InlineTable._render_dotted() renders it as a.b = {}.
The fix:
- A proxy returned by
OutOfOrderTableProxy.__getitem__now keeps a reference to the proxy it came from (_parent). _remove_table()also asks that parent to remove the table from the real part that holds it (_remove_child_table()). If that part becomes empty and other parts exist, it is removed in turn, through the same_remove_table(), so deeper prefixes work level by level.
With this the example writes x = { a.b.d = 2}, the same as the single-level case x = {b.c = 1, b.d = 2} already does. Proxies looked up from a real container have no parent, so their behavior does not change.
Tests: test_deleting_one_of_several_nested_dotted_keys_inside_inline_table covers deleting the first, last and middle key, deleting all of them (the table stays as a.b = {}), and a three-level prefix. All five cases fail on master and pass with the fix. The full suite passes (1489), pre-commit is clean, and mypy reports the same errors as on master. I also ran 20,000 random edit-and-dump runs against master and this branch: 6 runs that wrote invalid TOML on master pass here, and no run that passes on master fails here.
Fixes #731
Duplicate Check
- I searched the open PRs and found none that already fixes this issue.
- Related open PRs (if any, and why this PR is still needed): #616 and #692 also change how dotted keys inside inline tables render, but for adding keys, not deleting them. The example above still writes invalid TOML on both branches, and on #588 and #590.
Agent Drafting Metadata
- Agent: Claude Code
- Model: Claude Opus 5.5
- Notes: I reviewed the diff and the test results myself.