Skip to content

Commit

Permalink
Fix top level keys handling when building
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed May 19, 2021
1 parent f244322 commit 953fd3b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
18 changes: 18 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def test_build_example(example):

doc.add(nl())
doc.add(comment("Products"))
doc.add(nl())

products = aot()
doc["products"] = products
Expand Down Expand Up @@ -128,3 +129,20 @@ def test_append_table_after_multiple_indices():
"""
doc = parse(content)
doc.append("foobar", {"name": "John"})


def test_top_level_keys_are_put_at_the_root_of_the_document():
doc = document()
doc.add(comment("Comment"))
doc["foo"] = {"name": "test"}
doc["bar"] = 1

expected = """\
# Comment
bar = 1
[foo]
name = "test"
"""

assert doc.as_string()
4 changes: 1 addition & 3 deletions tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ def append(self, key, item): # type: (Union[Key, str, None], Item) -> Container
if isinstance(item, AoT) and self._body and not self._parsed:
if item and "\n" not in item[0].trivia.indent:
item[0].trivia.indent = "\n" + item[0].trivia.indent
else:
self.append(None, Whitespace("\n"))

if key is not None and key in self:
current_idx = self._map[key]
Expand Down Expand Up @@ -211,7 +209,7 @@ def append(self, key, item): # type: (Union[Key, str, None], Item) -> Container

if key_after is not None:
if isinstance(key_after, int):
if key_after + 1 < len(self._body) - 1:
if key_after + 1 < len(self._body):
return self._insert_at(key_after + 1, key, item)
else:
previous_item = self._body[-1][1]
Expand Down

0 comments on commit 953fd3b

Please sign in to comment.