Skip to content

fix invalid TOML from a comment set on an inline table value - #734

Open
rootvector2 wants to merge 1 commit into
python-poetry:masterfrom
rootvector2:inline-table-value-comment
Open

rootvector2 wants to merge 1 commit into
python-poetry:masterfrom
rootvector2:inline-table-value-comment

Conversation

@rootvector2

Copy link
Copy Markdown

Summary

A comment runs to the end of the line, so one written for a value inside an inline table swallows whatever follows it on that line:

>>> import tomlkit
>>> doc = tomlkit.parse("t = {a = 1, b = 2}\n")
>>> doc["t"]["b"].comment("note")
>>> print(tomlkit.dumps(doc))
t = {a = 1, b = 2# note}

tomllib rejects that with Unclosed inline table and tomlkit's own parser with UnexpectedCharError, so the document cannot be read back. A multiline inline table loses the separator comma the same way. It also happens for a nested inline table, an inline table inside an array, and a value under a dotted key inside an inline table, which is written by _render_dotted().

InlineTable.append() and InlineTable.__setitem__() already clear a value's comment when the value is inserted (#30, #31), so Item.comment() on a value that is already in the table is the path left over. Dropping the comment where the inline table is written also covers a value under a dotted key, whose super table is a plain Table and never goes through that insertion-time trim.

Parsing never puts a comment on a keyed value inside an inline table: in a multiline inline table the comment becomes its own Comment body element, which is still written. I checked that over the bundled toml-test corpus and tests/examples, so no parsed document changes, and a pair whose value carries a comment is now byte-identical to the same pair before the comment was set.

The new parametrized test covers the seven shapes above and fails on master for all of them. pytest tests is 1523 passed on Python 3.11 with the tests/toml-test submodule checked out; ruff check and ruff format --check are clean on the touched files, and mypy reports nothing that master does not.

Found with a script that makes random edits through the API and reads the output back with tomllib.

Duplicate Check

A comment runs to the end of the line, so one written for a value inside
an inline table swallows the separator comma or the closing brace, and
the document cannot be parsed back. `InlineTable.append()` and
`__setitem__()` already clear a value's comment on insertion, but one set
afterwards, as `Item.comment()` does, was still rendered by
`as_string()` and `_render_dotted()`.
@human-attestation

Copy link
Copy Markdown

🛡️ Human Attestation required. A human needs to confirm this PR once before it can merge.

Attest this PR — sign in with GitHub, pass the challenge, and confirm one of the statements.

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