Skip to content

Commit

Permalink
fix: Clear the existing table header (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Sep 26, 2022
1 parent 7194dae commit 5bc89ed
Show file tree
Hide file tree
Showing 4 changed files with 35 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

- Clear the existing table header when it is adding to another table. ([#230](https://github.com/sdispater/tomlkit/issues/230))

## [0.11.4] - 2022-08-12

### Fixed
Expand Down
29 changes: 29 additions & 0 deletions tests/test_toml_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,3 +1001,32 @@ def test_remove_item_from_super_table():
"""
assert doc.as_string() == dedent(expected)


def test_nested_table_update_display_name():
content = """\
[parent]
[parent.foo]
x = 1
"""

doc = parse(dedent(content))
sub = """\
[foo]
y = 2
[bar]
z = 3
"""
doc["parent"].update(parse(dedent(sub)))
expected = """\
[parent]
[parent.foo]
y = 2
[parent.bar]
z = 3
"""
assert doc.as_string() == dedent(expected)
2 changes: 1 addition & 1 deletion tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def append(self, key: Union[Key, str, None], item: Item) -> "Container":
prev = self._previous_item()
prev_ws = isinstance(prev, Whitespace) or ends_with_whitespace(prev)
if isinstance(item, Table):
if item.name != key.key:
if not self._parsed:
item.invalidate_display_name()
if self._body and not (self._parsed or item.trivia.indent or prev_ws):
item.trivia.indent = "\n"
Expand Down
2 changes: 1 addition & 1 deletion tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def item(
v.items(),
key=lambda i: (isinstance(i[1], dict), i[0] if _sort_keys else 1),
):
i = item(_v, _parent=a, _sort_keys=_sort_keys)
i = item(_v, _parent=table, _sort_keys=_sort_keys)
if isinstance(table, InlineTable):
i.trivia.trail = ""

Expand Down

0 comments on commit 5bc89ed

Please sign in to comment.