Skip to content

Commit 229ee86

Browse files
chuenchen309claudefrostming
authored
fix: don't reject an out-of-order super-table extended by a dotted key (#565)
`Container._validate_table_candidate` rejected valid TOML: when a deep header like `[a.b.c]` makes `a.b` an implicit super-table, reopening `[a]` and extending `a.b` via a dotted key (`b.z = 1`) was rejected with "Redefinition of an existing table". No table is defined twice -- `a.b` only gains a new key -- and stdlib `tomllib` (the validity oracle the tests already use) accepts it, producing `{'a': {'b': {'c': {}, 'z': 1}}}`. The `if k.is_dotted(): raise` guard fired before the super-table recursion that follows it, so it never got the chance to validate the subtree for a real conflict. Only reject the dotted case when the existing entry is *not* a super table; otherwise fall through to the existing recursion, which raises only on an actual redefinition. The concrete-table case (`[a.b]` then `[a]` `b.z = 1`) stays rejected, matching tomllib and the redefinition rejections from #516/#530. Adds a regression test next to the sibling out-of-order tests; it is red before the fix (ParseError) and green after. Full suite passes (372). Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Co-authored-by: Frost Ming <[email protected]>
1 parent 096148f commit 229ee86

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixed
66

7+
- Fix valid TOML being rejected when an out-of-order table reopens an implicit super-table with a dotted key: `[a.b.c]` then `[a]` with `b.z = 1` extends the implicit `a.b` super-table rather than redefining it (stdlib `tomllib` accepts it), so it now parses and round-trips instead of raising `Redefinition of an existing table`. The concrete-table case (`[a.b]` then `[a]` `b.z = 1`) stays rejected. ([#565](https://github.com/python-poetry/tomlkit/pull/565))
78
- Fix appending a key to a parsed inline table that uses `", "` separators emitting a bare `","` for the new entry, leaving the table inconsistently spaced (e.g. `{x = 1, y = 2,z = 3}`). ([#595](https://github.com/python-poetry/tomlkit/pull/595))
89

910
## [0.15.1] - 2026-07-17

‎tests/test_toml_document.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,17 @@ def test_valid_out_of_order_independent_tables() -> None:
664664
assert doc.as_string() == "[a]\nx=1\n[zz]\n[a.b]\nc=1\n"
665665

666666

667+
def test_valid_out_of_order_super_table_extended_by_dotted_key() -> None:
668+
# A deep header [a.b.c] makes a.b an implicit super-table; reopening [a] and
669+
# extending a.b via a dotted key (b.z) only adds a key to that super-table,
670+
# it is not a table redefinition. stdlib tomllib accepts this, so it must
671+
# parse and round-trip byte-for-byte rather than raise. (The concrete-table
672+
# form `[a.b]` then `[a]` `b.z=1` stays rejected, covered above.)
673+
doc = parse("[a.b.c]\n[a]\nb.z = 1\n")
674+
assert doc.unwrap() == {"a": {"b": {"c": {}, "z": 1}}}
675+
assert doc.as_string() == "[a.b.c]\n[a]\nb.z = 1\n"
676+
677+
667678
def test_set_value_on_out_of_order_table_with_empty_concrete_part() -> None:
668679
# A super table defined after its sub-table (the "defining a super-table
669680
# afterward is ok" spec example) leaves an empty concrete `[x]` part.

‎tomlkit/container.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,9 @@ def _validate_table_candidate(self, current: Table, candidate: Table) -> None:
430430
existing = current.value.item(k)
431431
if isinstance(existing, (Table, AoT)) != isinstance(v, (Table, AoT)):
432432
raise KeyAlreadyPresent(k)
433-
if k.is_dotted():
433+
if k.is_dotted() and not (
434+
isinstance(existing, Table) and existing.is_super_table()
435+
):
434436
raise TOMLKitError("Redefinition of an existing table")
435437
if isinstance(existing, Table) and isinstance(v, Table):
436438
if not existing.is_super_table() and not v.is_super_table():

0 commit comments

Comments
 (0)