Skip to content

Commit 7720273

Browse files
Fix ParseError when a sub-table extends the last array-of-tables element after an unrelated table
When a sub-table header such as [fruit.apple.texture] follows the array [[fruit]] with an unrelated table in between, the parser rebuilds it as a fresh super table named "fruit" and tries to append it to the document, colliding with the existing array of tables and raising ParseError: Key "fruit" already exists. even though the document is valid TOML 1.0.0. The contiguous form (no intervening table) already worked because the sub-table is consumed while the array element is still being parsed. A sub-table header that refers to an existing array of tables extends the array's last element, regardless of any tables defined in between, so merge its contents into that element instead of treating it as a redefinition. Defining a non-super table with the same name as an array (e.g. [fruit] after [[fruit]]) is still a genuine conflict and continues to raise. Fixes python-poetry#261
1 parent 8571bcd commit 7720273

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎CHANGELOG.md‎

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

1313
- Fix invalid serialization with a duplicated comma when removing a non-edge element from a parsed inline table. ([#486](https://github.com/python-poetry/tomlkit/pull/486))
1414
- Fix invalid serialization with a duplicated comma when appending or inserting into a comma-first formatted array. ([#499](https://github.com/python-poetry/tomlkit/pull/499))
15+
- Fix `ParseError` when a sub-table extends the last element of an array of tables after an unrelated table. ([#261](https://github.com/python-poetry/tomlkit/issues/261))
1516

1617
## [0.15.0] - 2026-05-10
1718

‎tests/test_toml_document.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,25 @@ def test_document_with_aot_after_sub_tables() -> None:
238238
assert doc["foo"]["bar"]["tests"][0]["name"] == "Test 1"
239239

240240

241+
def test_subtable_of_aot_element_after_other_table() -> None:
242+
# A sub-table header that extends the last element of an array of tables
243+
# is valid even when an unrelated table appears in between (issue #261).
244+
content = """[[fruit]]
245+
apple.color = "red"
246+
247+
[potato]
248+
249+
[fruit.apple.texture]
250+
smooth = true
251+
"""
252+
253+
doc = parse(content)
254+
255+
assert doc["fruit"][0]["apple"]["color"] == "red"
256+
assert doc["fruit"][0]["apple"]["texture"]["smooth"] is True
257+
assert doc["potato"] == {}
258+
259+
241260
def test_document_with_new_sub_table_after_other_table() -> None:
242261
content = """[foo]
243262
name = "Bar"

‎tomlkit/container.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,16 @@ def append(
226226
return self
227227
elif isinstance(current, AoT):
228228
if not item.is_aot_element():
229+
if item.is_super_table() and len(current.body):
230+
# A sub-table header such as `[fruit.apple.texture]`
231+
# appearing after the array `[[fruit]]` (possibly with
232+
# unrelated tables in between) extends the last element
233+
# of the array, per the TOML spec.
234+
last = current[-1]
235+
for k, v in item.value.body:
236+
last.value.append(k, v)
237+
238+
return self
229239
# Tried to define a table after an AoT with the same name.
230240
raise KeyAlreadyPresent(key)
231241

0 commit comments

Comments
 (0)