Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,14 @@ def test_deleting_inline_table_element_does_not_leave_trailing_separator2() -> N
assert table.as_string() == '{ baz = "boom"}'


def test_appending_to_parsed_inline_table_preserves_separator() -> None:
doc = parse("a = { foo = 1, bar = 2 }\n")
doc["a"]["baz"] = 3

assert doc.as_string() == "a = { foo = 1, bar = 2, baz = 3}\n"
parse(doc.as_string())


def test_booleans_comparison() -> None:
boolean = Bool(True, Trivia())

Expand Down
13 changes: 12 additions & 1 deletion tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,7 @@ def append(self, key: Key | str | None, _item: Any) -> InlineTable:
def as_string(self) -> str:
buf = "{"
emitted_key = False
needs_separator = False
has_explicit_commas = any(
k is None and isinstance(v, Whitespace) and "," in v.s
for k, v in self._value.body
Expand Down Expand Up @@ -2073,10 +2074,18 @@ def as_string(self) -> str:
elif not has_explicit_commas or "," in v.as_string():
buf = buf.rstrip(",")

buf += v.as_string()
v_string = v.as_string()
buf += v_string
if "," in v_string:
needs_separator = False

continue

if has_explicit_commas and needs_separator:
stripped = buf.rstrip()
buf = f"{stripped},{buf[len(stripped) :]}"
needs_separator = False

v_trivia_trail = v.trivia.trail.replace("\n", "")
buf += (
f"{v.trivia.indent}"
Expand All @@ -2087,6 +2096,8 @@ def as_string(self) -> str:
f"{v_trivia_trail}"
)
emitted_key = True
if has_explicit_commas:
needs_separator = True

if (
not has_explicit_commas
Expand Down
Loading