Start a new line before the closing bracket of a commented array - #582
Closed
dchaudhari7177 wants to merge 1 commit into
Closed
Start a new line before the closing bracket of a commented array#582dchaudhari7177 wants to merge 1 commit into
dchaudhari7177 wants to merge 1 commit into
Conversation
Array.as_string() renders a non-multiline array by concatenating its raw
items and appending "]". When add_line() was given a comment, the comment
was the last item, so the bracket landed inside it:
>>> a = tomlkit.array()
>>> a.add_line("foo", comment="bar")
>>> a.as_string()
'[\n "foo", # bar]'
The result no longer parses. Emit a newline (and the array's indent)
before the bracket when the last rendered item is a comment.
Fixes python-poetry#580
Contributor
|
why is this better or worse than the existing pull request #581? @dchaudhari7177 you should likely have your bot check for already-open pull requests before creating new ones |
Author
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.
Fixes #580.
Problem
The closing bracket sits inside the comment, so the output is no longer valid TOML and
tomlkit.loads()on the containing document raisesUnexpectedCharError.Cause
For a non-multiline array
as_string()is:add_line(comment=...)appends aCommentas the last item and nothing after it. A comment runs to the end of its line, so anything concatenated after it is commented out — including the bracket.Fix
When the last rendered item is a
Comment, emit a newline and the array's indent before the bracket, mirroring what the multiline branch already does.The issue suggests instead having
add_lineflip the array to multiline. I went the other way because the multiline branch re-renders with a fixed four-space indent and its own comma placement, which would throw away the layoutadd_lineexists to control precisely (add_line(1, 2, 3)would become one element per line). This change only adds the separator that is actually missing.Scope
Only affects arrays whose rendered items end with a comment. A parsed document can't reach that state — the parser has to consume the
], and a comment always ends at a newline, so a parsed array's last item is whitespace. So this is confined to arrays built through the API, and the existingtest_array_add_line(which ends withadd_line(indent="")) is unchanged, as is byte-for-byte round-tripping of parsed documents.Tests
test_array_add_line_trailing_comment_does_not_hide_bracket— the reported case, plus a re-parse of the containing document.test_array_add_line_trailing_comment_closing_line_is_not_doubled— callingadd_line(indent="")after a comment still produces exactly one closing line, not two.Full suite passes (1053 tests).
🤖 Generated with Claude Code