Skip to content

Commit de68fd0

Browse files
Fix ParseError when a sub-table extends an array of tables out of order (python-poetry#498)
* 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 * Document the out-of-order array-of-tables round-trip limitation --------- Co-authored-by: Frost Ming <[email protected]>
1 parent c46482b commit de68fd0

4 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎CHANGELOG.md‎

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

2222
- 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))
2323
- 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))
24+
- 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))
2425
- Fix unparseable serialization when adding a key to a dotted-key table inside an inline table. ([#500](https://github.com/python-poetry/tomlkit/pull/500))
2526
- Fix a table replaced by a plain value being serialized inside the preceding table's body when other tables follow; the value now moves before the first table like other root-level values. ([#504](https://github.com/python-poetry/tomlkit/issues/504))
2627
- Restore `dumps()` rendering mapping-like wrappers around a parsed document (e.g. `dotty_dict`'s `Dotty`) through their delegated `as_string`, preserving the original table order and layout instead of re-encoding through a plain dict — a 0.15.0 regression. ([#482](https://github.com/python-poetry/tomlkit/issues/482))

‎README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ Part of the implementation has been adapted, improved and fixed from [Molten](ht
2626

2727
See the [documentation](https://tomlkit.readthedocs.io/) for more information.
2828

29+
## Limitations
30+
31+
`tomlkit` preserves the original layout of a document, with one exception: a
32+
sub-table that extends an array of tables out of order — for example a
33+
`[fruit.apple.texture]` header that appears after `[[fruit]]` with an unrelated
34+
table in between — is normalized into the array's last element when the document
35+
is re-serialized, rather than kept at its original position. The data is
36+
preserved; only the physical placement of that sub-table changes.
37+
2938
## Installation
3039

3140
If you are using [uv](https://docs.astral.sh/uv), you can

‎tests/test_toml_document.py‎

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

241241

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

‎tomlkit/container.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,16 @@ def append(
260260
return self
261261
elif isinstance(current, AoT):
262262
if not item.is_aot_element():
263+
if item.is_super_table() and len(current.body):
264+
# A sub-table header such as `[fruit.apple.texture]`
265+
# appearing after the array `[[fruit]]` (possibly with
266+
# unrelated tables in between) extends the last element
267+
# of the array, per the TOML spec.
268+
last = current[-1]
269+
for k, v in item.value.body:
270+
last.value.append(k, v)
271+
272+
return self
263273
# Tried to define a table after an AoT with the same name.
264274
raise KeyAlreadyPresent(key)
265275

0 commit comments

Comments
 (0)