Skip to content

Commit 28fe6ec

Browse files
authored
fix: Empty table dumped when number of subtables > 1 (#378)
Fixes #377 Signed-off-by: Frost Ming <[email protected]>
1 parent 168cb22 commit 28fe6ec

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
- Fix the `Table.is_super_table()` check for tables with dotted key as the only child. ([#374](https://github.com/python-poetry/tomlkit/issues/374))
8+
- Count table as a super table if it has children and all children are either tables or arrays of tables. ([#377](https://github.com/python-poetry/tomlkit/issues/377))
89

910
## [0.13.0] - 2024-07-10
1011

tests/test_items.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,3 +993,16 @@ def test_serialize_table_with_dotted_key():
993993
parent = api.table()
994994
parent.add("a", child)
995995
assert parent.as_string() == "[a]\nb.c = 1\n"
996+
997+
998+
def test_not_showing_parent_header_for_super_table():
999+
doc = api.document()
1000+
1001+
def add_table(parent, name):
1002+
parent.add(name, api.table())
1003+
return parent[name]
1004+
1005+
root = add_table(doc, "root")
1006+
add_table(root, "first")
1007+
add_table(root, "second")
1008+
assert doc.as_string() == "[root.first]\n\n[root.second]\n"

tomlkit/items.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,21 +1621,23 @@ def is_super_table(self) -> bool:
16211621
If true, it won't appear in the TOML representation."""
16221622
if self._is_super_table is not None:
16231623
return self._is_super_table
1624-
# If the table has only one child and that child is a table, then it is a super table.
1625-
if len(self) != 1:
1624+
if not self:
16261625
return False
1627-
k, only_child = next(iter(self.items()))
1628-
if not isinstance(k, Key):
1629-
k = SingleKey(k)
1630-
index = self.value._map[k]
1631-
if isinstance(index, tuple):
1632-
return False
1633-
real_key = self.value.body[index][0]
1634-
return (
1635-
isinstance(only_child, (Table, AoT))
1636-
and real_key is not None
1637-
and not real_key.is_dotted()
1638-
)
1626+
# If the table has children and all children are tables, then it is a super table.
1627+
for k, child in self.items():
1628+
if not isinstance(k, Key):
1629+
k = SingleKey(k)
1630+
index = self.value._map[k]
1631+
if isinstance(index, tuple):
1632+
return False
1633+
real_key = self.value.body[index][0]
1634+
if (
1635+
not isinstance(child, (Table, AoT))
1636+
or real_key is None
1637+
or real_key.is_dotted()
1638+
):
1639+
return False
1640+
return True
16391641

16401642
def as_string(self) -> str:
16411643
return self._value.as_string()

0 commit comments

Comments
 (0)