Skip to content

Commit

Permalink
fix: Table header missing (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored Aug 2, 2024
1 parent b1b38b3 commit 8b288a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 12 additions & 2 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8b288a5

Please # to comment.