Keep single-line array bracket off a trailing comment line - #581
Open
youdie006 wants to merge 1 commit into
Open
Keep single-line array bracket off a trailing comment line#581youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
Array.as_string() concatenated the closing bracket directly after the joined items. When the last rendered element is a trailing comment (e.g. from add_line(value, comment=...)), the comment carries no newline, so the ']' landed on the comment line and the output no longer parsed. Move the bracket to its own line when the last element is a Comment. Genuine single-line arrays and multi-line arrays are unaffected.
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
tomlkit.array().add_line("foo", comment="bar").as_string()produced[\n "foo", # bar]— the closing]landed on the comment line, so the output failed to round-trip (tomlkit.loads(...)raisedUnexpectedCharError). Fixes #580.Array.as_string()'s single-line branch concatenated]directly after the joined items. A trailing comment (fromadd_line(..., comment=...)) carries no newline, so the bracket was swallowed by the comment. This moves the bracket onto its own line when the last rendered element is aComment; genuine single-line arrays ([1, 2, 3]) and multi-line arrays are byte-for-byte unchanged (the existingtest_array_add_lineassertion still passes).Note: the issue suggested making
add_linepromote single-line arrays to multiline, but that reformatsadd_line(1, 2, 3)onto separate lines and breakstest_array_add_line, so the fix belongs inas_string, notadd_line.Added a regression test (
test_array_add_line_single_line_comment_round_trips), verified red before the fix (UnexpectedCharError) and green after.pytest tests/test_items.py(87 passed) andruff format --checkpass locally.Agent Drafting Metadata
tests/test_items.py).