Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## [Unreleased]

### Fixed

- 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))

## [0.15.1] - 2026-07-17

### Changed
Expand Down
11 changes: 11 additions & 0 deletions tests/test_toml_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,17 @@ def test_valid_out_of_order_independent_tables() -> None:
assert doc.as_string() == "[a]\nx=1\n[zz]\n[a.b]\nc=1\n"


def test_valid_out_of_order_super_table_extended_by_dotted_key() -> None:
# A deep header [a.b.c] makes a.b an implicit super-table; reopening [a] and
# extending a.b via a dotted key (b.z) only adds a key to that super-table,
# it is not a table redefinition. stdlib tomllib accepts this, so it must
# parse and round-trip byte-for-byte rather than raise. (The concrete-table
# form `[a.b]` then `[a]` `b.z=1` stays rejected, covered above.)
doc = parse("[a.b.c]\n[a]\nb.z = 1\n")
assert doc.unwrap() == {"a": {"b": {"c": {}, "z": 1}}}
assert doc.as_string() == "[a.b.c]\n[a]\nb.z = 1\n"


def test_set_value_on_out_of_order_table_with_empty_concrete_part() -> None:
# A super table defined after its sub-table (the "defining a super-table
# afterward is ok" spec example) leaves an empty concrete `[x]` part.
Expand Down
4 changes: 3 additions & 1 deletion tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ def _validate_table_candidate(self, current: Table, candidate: Table) -> None:
existing = current.value.item(k)
if isinstance(existing, (Table, AoT)) != isinstance(v, (Table, AoT)):
raise KeyAlreadyPresent(k)
if k.is_dotted():
if k.is_dotted() and not (
isinstance(existing, Table) and existing.is_super_table()
):
raise TOMLKitError("Redefinition of an existing table")
if isinstance(existing, Table) and isinstance(v, Table):
if not existing.is_super_table() and not v.is_super_table():
Expand Down