Skip to content

Place inline-table separator after the value, not a trailing comment (fix #512)#514

Open
gaoflow wants to merge 1 commit into
python-poetry:masterfrom
gaoflow:fix-512-inline-table-comment-comma
Open

Place inline-table separator after the value, not a trailing comment (fix #512)#514
gaoflow wants to merge 1 commit into
python-poetry:masterfrom
gaoflow:fix-512-inline-table-comment-comma

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #512.

Problem

Appending a key to a multiline inline table whose last value is followed by a comment puts the separator comma inside the comment, producing output that no longer round-trips:

doc = tomlkit.loads("tbl = {\n    p = { k = 1 },\n    q = { k = 2 }  # comment\n}\n")
doc["tbl"]["added"] = 3
print(tomlkit.dumps(doc))
tbl = {
    p = { k = 1 },
    q = { k = 2 }  # comment,
added = 3}

The , is now part of the # comment line, so added has no separator and re-parsing the result raises a ParseError.

Cause

In InlineTable.as_string, the separator for a newly-appended key is inserted lazily when the next key is reached:

stripped = buf.rstrip()
buf = f"{stripped},{buf[len(stripped):]}"

buf.rstrip() strips trailing whitespace, but when the previous value is followed by a comment it lands after the comment text rather than after the value, so the comma is swallowed by the comment.

Fix

Track the buffer position right after each rendered value (last_value_end) and splice the deferred separator there. When there is no trailing comment this is exactly where buf.rstrip() pointed, so existing behaviour is unchanged; it only differs in the comment case. Output now round-trips:

tbl = {
    p = { k = 1 },
    q = { k = 2 },  # comment
added = 3}

Tests

Added test_appending_to_inline_table_with_trailing_comment_keeps_comma_outside_comment (asserts the comma is outside the comment and that the result round-trips). It fails on master and passes with the fix; the full suite (993 tests) stays green.

@gaoflow gaoflow force-pushed the fix-512-inline-table-comment-comma branch 2 times, most recently from fffc6cd to de0cdff Compare June 13, 2026 01:11
When appending a key to a multiline inline table whose last value is
followed by a comment, the deferred separator comma was spliced in with
buf.rstrip(), landing after the comment text instead of after the value.
The comma became part of the comment, so the new key had no separator
and the output no longer round-tripped. Track the buffer position right
after each rendered value and insert the separator there.

Fixes python-poetry#512.
@gaoflow gaoflow force-pushed the fix-512-inline-table-comment-comma branch from de0cdff to a0e5a87 Compare June 13, 2026 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

comma is appended to comment when adding to multiline inline table

1 participant