Skip to content

Use ", " separator when appending to a spaced inline table - #595

Open
Ashishjob wants to merge 2 commits into
python-poetry:masterfrom
Ashishjob:fix-inline-table-append-separator-spacing
Open

Use ", " separator when appending to a spaced inline table#595
Ashishjob wants to merge 2 commits into
python-poetry:masterfrom
Ashishjob:fix-inline-table-append-separator-spacing

Conversation

@Ashishjob

Copy link
Copy Markdown

When a key is appended to a parsed inline table whose entries are already separated with ", " (comma + space), the deferred separator inserted for the new key was a bare "," with no space, leaving the table inconsistently spaced:

>>> from tomlkit import parse
>>> doc = parse("a = {x = 1, y = 2}\n")
>>> doc["a"]["z"] = 3
>>> doc.as_string()
'a = {x = 1, y = 2,z = 3}\n'   # note the "2,z"

The existing entries use ", ", and inline tables built from scratch also use ", ", so the new separator is inconsistent.

Fix

Insert ", " to match the surrounding style — but only when the text that will follow the comma (the previous value’s trailing trivia plus the new key’s own indent) does not already start with a space. This keeps a padded table such as { foo = 1, bar = 2 } (whose appended key already contributes a space via its indent) from becoming double-spaced. This is a follow-up to #477, which fixed the missing comma but emitted it without the conventional space.

Result:

>>> doc.as_string()
'a = {x = 1, y = 2, z = 3}\n'

Padded tables are unchanged: { foo = 1, bar = 2 } + baz still renders { foo = 1, bar = 2, baz = 3}.

Tests

Added test_appending_to_compact_inline_table_uses_spaced_separator; verified it fails before the change and passes after. Full suite green (373 passed, test_toml_tests.py needs the vendored submodule and was not run).

I can add a CHANGELOG entry once you confirm the target section (there is no Unreleased section currently).

Ashishjob and others added 2 commits September 2, 2026 02:57
When a key is appended to a parsed inline table that already separates its
entries with ", " (comma + space), the deferred separator inserted for the new
key was a bare "," with no space. This left the table inconsistently spaced:

    >>> doc = parse("a = {x = 1, y = 2}\n")
    >>> doc["a"]["z"] = 3
    >>> doc.as_string()
    'a = {x = 1, y = 2,z = 3}\n'   # note "2,z"

Insert ", " to match the surrounding style, but only when the text that will
follow the comma (the previous value's trailing trivia plus the new key's own
indent) does not already begin with a space -- so a padded table like
`{ foo = 1, bar = 2 }` keeps producing a single space rather than a double one.

Added a regression test for the compact-with-spaced-separators case.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…etry#595)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

1 participant