From 6a6dde66e14878ae767daa08c16a7fa3deaaaa0f Mon Sep 17 00:00:00 2001 From: Ashishjob Date: Wed, 2 Sep 2026 02:57:19 -0500 Subject: [PATCH 1/2] Use ", " separator when appending to a spaced inline table 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 --- tests/test_items.py | 11 +++++++++++ tomlkit/items.py | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/test_items.py b/tests/test_items.py index e331caeb..cfbe2e1a 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -979,6 +979,17 @@ def test_appending_to_parsed_inline_table_preserves_separator() -> None: parse(doc.as_string()) +def test_appending_to_compact_inline_table_uses_spaced_separator() -> None: + # A compact inline table separates its existing entries with ", " (comma + + # space). A key appended after parsing must use the same spacing, instead of + # a bare "," that leaves the table inconsistently spaced ("y = 2,z = 3"). + doc = parse("a = {x = 1, y = 2}\n") + doc["a"]["z"] = 3 + + assert doc.as_string() == "a = {x = 1, y = 2, z = 3}\n" + assert parse(doc.as_string()) == {"a": {"x": 1, "y": 2, "z": 3}} + + def test_append_key_after_inline_table_trailing_comment() -> None: doc = parse("tbl = {\n p = { k = 1 },\n q = { k = 2 } # comment\n}\n") doc["tbl"]["added"] = 3 diff --git a/tomlkit/items.py b/tomlkit/items.py index 31369b03..79b3bdb2 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -2117,7 +2117,15 @@ def as_string(self) -> str: # Insert the deferred separator right after the previous value, # not after any trailing comment/whitespace -- otherwise the # comma is swallowed by a trailing comment (see #512). - buf = f"{buf[:last_value_end]},{buf[last_value_end:]}" + # Match the conventional ", " spacing used by the explicit + # separators already in the table. Only add the space 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 one, so a padded table like ``{ a = 1 }`` -- which + # contributes the space itself -- does not end up double-spaced. + following = buf[last_value_end:] + v.trivia.indent + separator = "," if following.startswith(" ") else ", " + buf = f"{buf[:last_value_end]}{separator}{buf[last_value_end:]}" needs_separator = False v_trivia_trail = v.trivia.trail.replace("\n", "") From 306d406b38778c7a24a7fdcd118ed5c17887fb37 Mon Sep 17 00:00:00 2001 From: Ashishjob Date: Wed, 2 Sep 2026 02:58:05 -0500 Subject: [PATCH 2/2] Add CHANGELOG entry for inline table separator spacing fix (#595) Co-Authored-By: Claude Opus 4.8 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3737b80e..edea7022 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## [Unreleased] + +### Fixed + +- Fix appending a key to a parsed inline table that uses `", "` separators emitting a bare `","` for the new entry, leaving the table inconsistently spaced (e.g. `{x = 1, y = 2,z = 3}`). ([#595](https://github.com/python-poetry/tomlkit/pull/595)) + ## [0.15.1] - 2026-07-17 ### Changed