Skip to content

Keep valid TOML when deleting the last dotted key in an inline table - #687

Closed
mayur-dhavan wants to merge 2 commits into
python-poetry:masterfrom
mayur-dhavan:fix-inline-dotted-delete
Closed

mayur-dhavan wants to merge 2 commits into
python-poetry:masterfrom
mayur-dhavan:fix-inline-dotted-delete

Conversation

@mayur-dhavan

Copy link
Copy Markdown

Fixes #662.

Problem

Deleting the last child of a dotted key inside an inline table emitted nothing for the key, leaving a dangling separator:

d = tomlkit.parse('t = {a="", b.c=1, d=1}\n')
del d["t"]["b"]["c"]
tomlkit.dumps(d)   # 't = {a="", , d=1}\n'  - rejected by tomllib

Depending on the key's position this produced a leading comma ({, a = 1}), a trailing comma ({a = 1, }), or silently dropped b when it was the only key.

Fix

In InlineTable._render_dotted (tomlkit/items.py), a dotted key whose children were all removed now renders as an empty inline table instead of nothing, both at the top level and for nested dotted children:

  • t = {a="", b.c=1, d=1} -> t = {a="", b={}, d=1}
  • t = {a="", b.c.d=1, e=1} (delete d) -> t = {a="", b.c={}, e=1}

The document model still holds the emptied table after the deletion, so rendering it as {} keeps the output faithful and parseable.

Tests

Three regression tests in tests/test_toml_document.py (middle/first/last/only-key positions, nested dotted child), each asserting the exact output and, where available, a tomllib round-trip. Full suite passes locally: 1206 passed (includes the toml-test cases).

AI disclosure: this contribution was prepared with AI assistance (an automated coding agent acting on behalf of the author); the change and tests were validated locally against the project's test suite as described above.

Copilot AI balanced review requested due to automatic review settings October 25, 2026 18:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@mayur-dhavan

Copy link
Copy Markdown
Author

Note on the failing check: Tests / ubuntu-latest / 3.9 fails during setup with "The version '3.9' with architecture 'x64' was not found for Ubuntu 26.04." This is a repo-wide CI environment issue (the ubuntu-latest runner image rolled to 26.04, where setup-python no longer provides 3.9) - other recent PRs (#685, #686) fail the same job identically. The remaining 24 checks pass.

@dimbleby

Copy link
Copy Markdown
Contributor

Explain why not #664

@mayur-dhavan

Copy link
Copy Markdown
Author

Honest answer: there isn't a good reason - I missed #664 when checking for existing work on #662. Comparing the two now: same root cause, same approach (render the emptied dotted-key table as an empty inline table), equivalent coverage of all separator positions and the nested b.c.d case, and #664 additionally includes a changelog entry and is green on CI. Since #664 came first and covers the same ground, I'm closing this one in its favor. Sorry for the noise.

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 the last key under a dotted key in an inline table produces invalid TOML

3 participants