Skip to content

Fix duplicate table header when adding a key to an out-of-order table#545

Open
sarathfrancis90 wants to merge 1 commit into
python-poetry:masterfrom
sarathfrancis90:fix-out-of-order-table-duplicate-header
Open

Fix duplicate table header when adding a key to an out-of-order table#545
sarathfrancis90 wants to merge 1 commit into
python-poetry:masterfrom
sarathfrancis90:fix-out-of-order-table-duplicate-header

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

While editing a parsed document I hit invalid output from a plain assignment. If a table is split out of order so its concrete [x] header comes after its sub-tables (the "defining a super-table afterward is ok" spec form), adding a key to it writes a second [x] header and the result no longer parses:

doc = tomlkit.parse("[x.y.z.w]\n\n[x]\n")
doc["x"]["c"] = 3
tomlkit.parse(doc.as_string())  # ParseError: Key "x" already exists

The out-of-order proxy was putting the new value in the header-less super part (the one created by [x.y.z.w]), which then had to emit its own [x] header next to the existing empty concrete [x]. I changed the plain-value fallback to route into the existing concrete (non-super) part instead, so the header that's already there gains the key and no duplicate header is written.

Added a regression test. The full pytest suite is green and the change is ruff / ruff format clean.

…r table

Adding a plain key to an out-of-order table whose concrete `[x]` part is
declared after its sub-tables (the "defining a super-table afterward is ok"
spec form) wrote the value into the header-less super part, forcing it to
render a second `[x]` header next to the existing concrete one. The dumped
output then had a duplicate header and no longer parsed.

Route the value into the existing concrete (non-super) part instead, so the
header that already exists gains the key and no second header is emitted.
@sarathfrancis90 sarathfrancis90 force-pushed the fix-out-of-order-table-duplicate-header branch from 4ff9bc2 to 1cf87f8 Compare June 26, 2026 06:34
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.

1 participant