diff --git a/tests/test_toml_document.py b/tests/test_toml_document.py index 967aa65..4dea552 100644 --- a/tests/test_toml_document.py +++ b/tests/test_toml_document.py @@ -1293,3 +1293,24 @@ def test_parse_aot_without_ending_newline(): "foo": {}, "bar": {}, } + + +def test_appending_to_super_table(): + content = """\ +[a.b] +value = 5 +""" + + doc = parse(content) + table_a = doc["a"] + table_a.append(tomlkit.key(["c", "d"]), "foo") + + expected = """\ +[a] +c.d = "foo" + +[a.b] +value = 5 +""" + + assert doc.as_string() == expected diff --git a/tomlkit/container.py b/tomlkit/container.py index 35f8aec..9fb449b 100644 --- a/tomlkit/container.py +++ b/tomlkit/container.py @@ -521,12 +521,19 @@ def _render_table(self, key: Key, table: Table, prefix: str | None = None) -> st if prefix is not None: _key = prefix + "." + _key - if not table.is_super_table() or ( - any( - not isinstance(v, (Table, AoT, Whitespace, Null)) - for _, v in table.value.body + if ( + not table.is_super_table() + or ( + any( + not isinstance(v, (Table, AoT, Whitespace, Null)) + for _, v in table.value.body + ) + and not key.is_dotted() + ) + or ( + any(k.is_dotted() for k, v in table.value.body if isinstance(v, Table)) + and not key.is_dotted() ) - and not key.is_dotted() ): open_, close = "[", "]" if table.is_aot_element():