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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
Loading