Skip to content

Commit 5bc89ed

Browse files
authored
fix: Clear the existing table header (#234)
1 parent 7194dae commit 5bc89ed

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Clear the existing table header when it is adding to another table. ([#230](https://github.com/sdispater/tomlkit/issues/230))
8+
59
## [0.11.4] - 2022-08-12
610

711
### Fixed

tests/test_toml_document.py

+29
Original file line numberDiff line numberDiff line change
@@ -1001,3 +1001,32 @@ def test_remove_item_from_super_table():
10011001
10021002
"""
10031003
assert doc.as_string() == dedent(expected)
1004+
1005+
1006+
def test_nested_table_update_display_name():
1007+
content = """\
1008+
[parent]
1009+
1010+
[parent.foo]
1011+
x = 1
1012+
"""
1013+
1014+
doc = parse(dedent(content))
1015+
sub = """\
1016+
[foo]
1017+
y = 2
1018+
1019+
[bar]
1020+
z = 3
1021+
"""
1022+
doc["parent"].update(parse(dedent(sub)))
1023+
expected = """\
1024+
[parent]
1025+
1026+
[parent.foo]
1027+
y = 2
1028+
1029+
[parent.bar]
1030+
z = 3
1031+
"""
1032+
assert doc.as_string() == dedent(expected)

tomlkit/container.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def append(self, key: Union[Key, str, None], item: Item) -> "Container":
194194
prev = self._previous_item()
195195
prev_ws = isinstance(prev, Whitespace) or ends_with_whitespace(prev)
196196
if isinstance(item, Table):
197-
if item.name != key.key:
197+
if not self._parsed:
198198
item.invalidate_display_name()
199199
if self._body and not (self._parsed or item.trivia.indent or prev_ws):
200200
item.trivia.indent = "\n"

tomlkit/items.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def item(
205205
v.items(),
206206
key=lambda i: (isinstance(i[1], dict), i[0] if _sort_keys else 1),
207207
):
208-
i = item(_v, _parent=a, _sort_keys=_sort_keys)
208+
i = item(_v, _parent=table, _sort_keys=_sort_keys)
209209
if isinstance(table, InlineTable):
210210
i.trivia.trail = ""
211211

0 commit comments

Comments
 (0)