Skip to content

Commit b9d22fb

Browse files
authored
fix: Add DottedKey to a super table gives wrong output (#431) (#435)
When a table is already a super table, appending dotted items to it will result in wrong behavior. The header was missing, so the final result was an incorrect representation of the document. Because super_table is already set and can no longer be re-evaluated, I have added a check to the renderer instead, so that it can determine if the header should be rendered. I have not gone through different edge cases, but it seems that all other tests are still passing. Co-authored-by: deltamarnix <[email protected]>
1 parent fe45e51 commit b9d22fb

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

tests/test_toml_document.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,3 +1293,24 @@ def test_parse_aot_without_ending_newline():
12931293
"foo": {},
12941294
"bar": {},
12951295
}
1296+
1297+
1298+
def test_appending_to_super_table():
1299+
content = """\
1300+
[a.b]
1301+
value = 5
1302+
"""
1303+
1304+
doc = parse(content)
1305+
table_a = doc["a"]
1306+
table_a.append(tomlkit.key(["c", "d"]), "foo")
1307+
1308+
expected = """\
1309+
[a]
1310+
c.d = "foo"
1311+
1312+
[a.b]
1313+
value = 5
1314+
"""
1315+
1316+
assert doc.as_string() == expected

tomlkit/container.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,19 @@ def _render_table(self, key: Key, table: Table, prefix: str | None = None) -> st
521521
if prefix is not None:
522522
_key = prefix + "." + _key
523523

524-
if not table.is_super_table() or (
525-
any(
526-
not isinstance(v, (Table, AoT, Whitespace, Null))
527-
for _, v in table.value.body
524+
if (
525+
not table.is_super_table()
526+
or (
527+
any(
528+
not isinstance(v, (Table, AoT, Whitespace, Null))
529+
for _, v in table.value.body
530+
)
531+
and not key.is_dotted()
532+
)
533+
or (
534+
any(k.is_dotted() for k, v in table.value.body if isinstance(v, Table))
535+
and not key.is_dotted()
528536
)
529-
and not key.is_dotted()
530537
):
531538
open_, close = "[", "]"
532539
if table.is_aot_element():

0 commit comments

Comments
 (0)