Skip to content

Commit eb7d324

Browse files
frostmingsdispater
authored andcommitted
Trim comments when setting InlineTable (#31)
* Trim comments when setting InlineTable InlineTable is supposed to be one liner and doesn't support comments in values. * Add tests * Fix coverage
1 parent 9856625 commit eb7d324

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

tests/test_items.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,14 @@ def test_items_are_pickable():
417417

418418
s = pickle.dumps(n)
419419
assert pickle.loads(s).as_string() == 'foo = "bar"\n'
420+
421+
422+
def test_trim_comments_when_building_inline_table():
423+
table = inline_table()
424+
row = parse('foo = "bar" # Comment')
425+
table.update(row)
426+
assert table.as_string() == '{foo = "bar"}'
427+
value = item("foobaz")
428+
value.comment("Another comment")
429+
table.append("baz", value)
430+
assert "# Another comment" not in table.as_string()

tomlkit/items.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,8 @@ def append(self, key, _item): # type: (Union[Key, str], Any) -> InlineTable
938938
if not isinstance(_item, (Whitespace, Comment)):
939939
if not _item.trivia.indent and len(self._value) > 0:
940940
_item.trivia.indent = " "
941+
if _item.trivia.comment:
942+
_item.trivia.comment = ""
941943

942944
self._value.append(key, _item)
943945

@@ -1017,6 +1019,8 @@ def __setitem__(self, key, value): # type: (Union[Key, str], Any) -> None
10171019

10181020
if key is not None:
10191021
super(InlineTable, self).__setitem__(key, value)
1022+
if value.trivia.comment:
1023+
value.trivia.comment = ""
10201024

10211025
m = re.match("(?s)^[^ ]*([ ]+).*$", self._trivia.indent)
10221026
if not m:

0 commit comments

Comments
 (0)