Skip to content

Fix invalid TOML after deleting a nested dotted key in an inline table - #732

Merged
frostming merged 2 commits into
python-poetry:masterfrom
zachyang374:fix/inline-nested-dotted-delete
Dec 4, 2026
Merged

frostming merged 2 commits into
python-poetry:masterfrom
zachyang374:fix/inline-nested-dotted-delete

Conversation

@zachyang374

Copy link
Copy Markdown
Contributor

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

Agent Drafting Metadata

  • Agent: Claude Code
  • Model: Claude Opus 5.5
  • Notes: I reviewed the diff and the test results myself.

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 <noreply@anthropic.com>
@human-attestation

human-attestation Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

✅ Human Attestation: @zachyang374 attested this PR at commit 4ef7959 (2026-12-04T06:52:06.831Z).

I have read and understood all agent-written code in this PR. / 我已阅读并理解此 PR 中 Agent 编写的全部代码。

Later pushes to this PR stay attested. Attestation page

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@frostming
frostming merged commit 87fc86a into python-poetry:master Dec 4, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting one of several nested dotted keys in an inline table writes invalid TOML

2 participants