Skip to content

Commit e23a254

Browse files
fix: avoid duplicate table header when adding a key to an out-of-order table (#545)
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. Co-authored-by: Frost Ming <[email protected]>
1 parent 34e51e2 commit e23a254

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- Fix a `KeyAlreadyPresent` error when parsing or accessing an out-of-order table whose array-of-tables elements are split across the table's parts. ([#505](https://github.com/python-poetry/tomlkit/issues/505))
3535
- Out-of-order value-vs-table and dotted-key-vs-table redefinitions are now rejected at parse time instead of being silently accepted or raising only on access. The parser also detects when a non-dotted key is a prefix of an existing dotted key, matching the stdlib `tomllib` behaviour. ([#523](https://github.com/python-poetry/tomlkit/issues/523))
3636
- Reject tables inserted into inline tables instead of serializing invalid TOML. ([#531](https://github.com/python-poetry/tomlkit/issues/531))
37+
- Fix invalid serialization with a duplicated `[table]` header when adding a key to an out-of-order table whose concrete header is declared after its sub-tables; the new key now lands in the existing concrete part instead of giving the header-less super part a second header. ([#545](https://github.com/python-poetry/tomlkit/pull/545))
3738
- Fix a table's display name (its exact header spelling, including whitespace and quoting) being normalised when the table is assigned onto itself, e.g. `doc[k] = doc[k]` rewriting `[keys .'a'.'c']` to `[keys.a.'c']`. ([#291](https://github.com/python-poetry/tomlkit/issues/291))
3839
- Fix missing newlines when appending a key after a dotted inline table, including when the original document has no trailing newline. ([#533](https://github.com/python-poetry/tomlkit/pull/533))
3940
- Preserve trailing whitespace when replacing a super table, including assigning it onto itself. ([#534](https://github.com/python-poetry/tomlkit/pull/534))

‎tests/test_toml_document.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,20 @@ def test_valid_out_of_order_independent_tables() -> None:
643643
assert doc.as_string() == "[a]\nx=1\n[zz]\n[a.b]\nc=1\n"
644644

645645

646+
def test_set_value_on_out_of_order_table_with_empty_concrete_part() -> None:
647+
# A super table defined after its sub-table (the "defining a super-table
648+
# afterward is ok" spec example) leaves an empty concrete `[x]` part.
649+
# Adding a plain value must land in that concrete part, not turn the
650+
# header-less super part into a second `[x]` header -- which produced
651+
# output with a duplicate header that no longer parsed.
652+
doc = parse("[x.y.z.w]\n\n[x]\n")
653+
doc["x"]["c"] = 3
654+
655+
assert doc["x"]["c"] == 3
656+
assert doc.as_string() == "[x.y.z.w]\n\n[x]\nc = 3\n"
657+
assert parse(doc.as_string()).unwrap() == {"x": {"y": {"z": {"w": {}}}, "c": 3}}
658+
659+
646660
def test_out_of_order_table_merges_aot_fragments() -> None:
647661
# https://github.com/python-poetry/tomlkit/issues/505
648662
content = """\

‎tomlkit/container.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,18 @@ def _is_table_or_aot(it: Any) -> bool:
11741174
table[key] = value
11751175
break
11761176
else:
1177-
self._tables[0][key] = value
1177+
# No part holds a plain value yet, so the chosen part must
1178+
# start rendering its own ``[key]`` header. Prefer a part
1179+
# that is not a super table (it already renders that header):
1180+
# turning a header-less super part concrete here would emit a
1181+
# second, duplicate header next to an existing concrete part
1182+
# and produce TOML that no longer parses.
1183+
for table in self._tables:
1184+
if not table.is_super_table():
1185+
table[key] = value
1186+
break
1187+
else:
1188+
self._tables[0][key] = value
11781189
else:
11791190
self._tables[0][key] = value
11801191
else:

0 commit comments

Comments
 (0)