tomlkit
tomlkit copied to clipboard
unwrap raises KeyAlreadyPresent for an out-of-order AoT element extension
Description
tomlkit.parse() accepts and exactly round-trips this valid document, but accessing doc["y"] or calling doc.unwrap() raises KeyAlreadyPresent: Key "a" already exists:
from tomlkit import parse
content = """\
[[y.a]]
[b.d.x]
[[y.a.c]]
"""
doc = parse(content)
assert doc.as_string() == content
doc.unwrap() # KeyAlreadyPresent: Key "a" already exists
Python's standard-library tomllib reads it as:
{"y": {"a": [{"c": [{}]}]}, "b": {"d": {"x": {}}}}
The later [[y.a.c]] header extends the last element of the existing y.a array of tables. This works when the parts are adjacent, but an unrelated table makes y out of order. OutOfOrderTableProxy then sees a first as an AoT and later as an implicit Table; its existing fragment merge handles AoT + AoT, but not this AoT + super-table extension.
The example was initially flagged by @chuenchen309 in the description of #565. I reproduced it on current master and narrowed it down to the proxy merge path.
Environment
- tomlkit 0.15.1 / current
master - Python 3.12