Fix single-line Array losing its closing bracket after a trailing comment - #602
Open
afonsojanu wants to merge 1 commit into
Open
afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
…ment
Array.as_string() for a non-multiline array just concatenates the raw
text of each item, including any comment added through add_line(). A
"#" comment runs to the end of its physical line, so when the comment
happens to be the last thing rendered before "]", the bracket gets
swallowed into the comment and the array no longer parses.
>>> a = tomlkit.array()
>>> a.add_line("foo", comment="bar")
>>> a.as_string()
'[\n "foo", # bar]' # note: no closing bracket outside the comment
Force a newline before the bracket whenever the rendered content would
otherwise end with an unterminated comment, so the array keeps
round-tripping through tomlkit.loads().
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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
Array.as_string()for a non-multiline array renders by concatenating the raw text of each item, including any comment attached throughadd_line(). Since a#comment runs to the end of its physical line, if that comment happens to be the last thing rendered before the closing], the bracket gets swallowed into the comment text and the array stops being valid TOML:This matches #580: the existing workaround is to always finish with a manual
add_line(indent="")call to supply the missing newline yourself, which is easy to forget and produces a document that silently fails to round-trip.Fix
In the non-multiline branch of
Array.as_string(), if the rendered content ends with a comment and isn't already terminated by a newline, a newline (plus the array's indent) is appended before the closing bracket. This only changes output for the specific case that was broken; arrays without a trailing comment, and the already-documented workaround, render exactly as before.Testing
Added
test_array_add_line_with_comment_on_last_line_round_tripsintests/test_items.py, which reproduces the issue and asserts the array parses back correctly. Ran the full suite (pytest tests/ --ignore=tests/test_toml_tests.py, the ignored module needs a submodule I didn't have checked out) plusruff check/ruff format --checkon the touched files; everything passes.Fixes #580.
Agent Drafting Metadata
tomlkit/items.py, and add the regression test. I reviewed the diff, ran the test suite and linters myself, and wrote this description.