Repository navigation
fix invalid TOML from a comment set on an inline table value - #734
Open
rootvector2 wants to merge 1 commit into
Open
rootvector2 wants to merge 1 commit into
rootvector2 wants to merge 1 commit into
Conversation
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 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
tomllibrejects that withUnclosed inline tableand tomlkit's own parser withUnexpectedCharError, 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()andInlineTable.__setitem__()already clear a value's comment when the value is inserted (#30, #31), soItem.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 plainTableand 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
Commentbody element, which is still written. I checked that over the bundled toml-test corpus andtests/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
masterfor all of them.pytest testsis 1523 passed on Python 3.11 with thetests/toml-testsubmodule checked out;ruff checkandruff format --checkare clean on the touched files, andmypyreports nothing thatmasterdoes not.Found with a script that makes random edits through the API and reads the output back with
tomllib.Duplicate Check
_render_dotted()that writesv.trivia.commenttoo, so that branch needs the same drop if it lands first.