From 8b288a5b1483834b0dd4c7b689033b41b57e2084 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Fri, 2 Aug 2024 09:16:24 +0800 Subject: [PATCH] fix: Table header missing (#375) --- CHANGELOG.md | 4 ++++ tests/test_items.py | 8 ++++++++ tomlkit/items.py | 14 ++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 566aab6..7e6fa61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [unreleased] +### Fixed + +- 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)) + ## [0.13.0] - 2024-07-10 ### Changed diff --git a/tests/test_items.py b/tests/test_items.py index 8d6723e..4a82948 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -985,3 +985,11 @@ def test_no_extra_minus_sign(): assert doc.as_string() == "a = +1.5" doc["a"] *= -1 assert doc.as_string() == "a = -1.5" + + +def test_serialize_table_with_dotted_key(): + child = api.table() + child.add(api.key(("b", "c")), 1) + parent = api.table() + parent.add("a", child) + assert parent.as_string() == "[a]\nb.c = 1\n" diff --git a/tomlkit/items.py b/tomlkit/items.py index d02fca1..538b72e 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -1624,8 +1624,18 @@ def is_super_table(self) -> bool: # If the table has only one child and that child is a table, then it is a super table. if len(self) != 1: return False - only_child = next(iter(self.values())) - return isinstance(only_child, (Table, AoT)) + k, only_child = next(iter(self.items())) + if not isinstance(k, Key): + k = SingleKey(k) + index = self.value._map[k] + if isinstance(index, tuple): + return False + real_key = self.value.body[index][0] + return ( + isinstance(only_child, (Table, AoT)) + and real_key is not None + and not real_key.is_dotted() + ) def as_string(self) -> str: return self._value.as_string()