diff --git a/CHANGELOG.md b/CHANGELOG.md index d2e8c49c..9a365353 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - `item()` and `dumps()` now write the underlying characters of a `str` subclass instead of calling its `__str__` method, so `str`-based `Enum` members and custom string wrappers round-trip again instead of being serialized as e.g. `"Color.RED"` or producing invalid TOML. This was a regression from the single-pass escaping added in 0.15.3. ([#725](https://github.com/python-poetry/tomlkit/issues/725)) - The parser now rejects numbers with non-ASCII digits, such as `1٠` or `10`. Previously `int()` and `float()` converted them, so an invalid document parsed to a wrong value. ([#728](https://github.com/python-poetry/tomlkit/pull/728)) - Fix deleting one of several nested dotted keys inside an inline table writing invalid TOML: deleting `c` from `x = {a.b.c = 1, a.b.d = 2}` gave `x = {a.b = {}, a.b.d = 2}`, which defines `a.b` twice. The emptied part is now removed from the document, as it already was for a single-level dotted key. ([#732](https://github.com/python-poetry/tomlkit/pull/732)) +- A comment set on a value that is already inside an inline table, e.g. with `Item.comment()`, is no longer written: it ran to the end of the line and swallowed the separator or the closing brace, as in `t = {a = 1# note, b = 2}`, so the document could not be parsed back. A comment on a value inserted into an inline table was already dropped. ([#30](https://github.com/python-poetry/tomlkit/issues/30)) ## [0.15.3] - 2026-11-24 diff --git a/tests/test_items.py b/tests/test_items.py index fee4b68e..c54aba90 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -1337,6 +1337,32 @@ def test_trim_comments_when_building_inline_table() -> None: assert table.as_string() == '{foo = "bar", baz = "foobaz"}' +@pytest.mark.parametrize( + ("content", "path"), + [ + ("t = {a = 1, b = 2}\n", ["t", "a"]), + ("t = {a = 1, b = 2}\n", ["t", "b"]), + ("t = { a = 1 }\n", ["t", "a"]), + ("a = {b = {c = 1}}\n", ["a", "b", "c"]), + ("a = [{x = 1}, {y = 2}]\n", ["a", 0, "x"]), + ("t = {a.b = 1, a.c = 2}\n", ["t", "a", "b"]), + ("t = {\n a = 1,\n b = 2\n}\n", ["t", "a"]), + ], +) +def test_trim_comments_set_on_a_value_already_in_an_inline_table( + content: str, path: list[Any] +) -> None: + doc = parse(content) + target: Any = doc + for part in path: + target = target[part] + target.comment("note") + + # A comment here would run past the separator and the closing brace. + assert doc.as_string() == content + assert parse(doc.as_string()) == doc + + def test_comment_method_multiline_comment_is_rejected() -> None: doc = api.document() doc["x"] = 1 diff --git a/tomlkit/items.py b/tomlkit/items.py index 9d6cd607..99b4dd01 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -2375,7 +2375,11 @@ def as_string(self) -> str: ) buf += f"{v.trivia.indent}{rendered}" last_value_end = len(buf) - buf += f"{v.trivia.comment}{v_trivia_trail}" + # A comment runs to the end of the line, so writing one for a value + # here would swallow the separator or the closing brace. append() + # and __setitem__() drop a comment when the value is inserted; one + # set on the value afterwards has to be dropped here as well. + buf += v_trivia_trail emitted_key = True pending_separator = False if has_explicit_commas: @@ -2407,9 +2411,12 @@ def _render_dotted(self, key: Key, table: Table) -> list[str]: parts.extend(f"{prefix}{sub}" for sub in self._render_dotted(k, v)) else: trail = v.trivia.trail.replace("\n", "") + # Dropped for the same reason as in as_string(), along with the + # whitespace that only separates the value from the comment. + comment_ws = "" if v.trivia.comment else v.trivia.comment_ws parts.append( f"{prefix}{v.trivia.indent}{k.as_string()}{k.sep}{v.as_string()}" - f"{v.trivia.comment_ws}{v.trivia.comment}{trail}" + f"{comment_ws}{trail}" ) if not parts: # every key under the prefix was removed, keep it as an empty table