Place inline-table separator after the value, not a trailing comment (fix #512)#514
Open
gaoflow wants to merge 1 commit into
Open
Place inline-table separator after the value, not a trailing comment (fix #512)#514gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
fffc6cd to
de0cdff
Compare
When appending a key to a multiline inline table whose last value is followed by a comment, the deferred separator comma was spliced in with buf.rstrip(), landing after the comment text instead of after the value. The comma became part of the comment, so the new key had no separator and the output no longer round-tripped. Track the buffer position right after each rendered value and insert the separator there. Fixes python-poetry#512.
de0cdff to
a0e5a87
Compare
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 #512.
Problem
Appending a key to a multiline inline table whose last value is followed by a comment puts the separator comma inside the comment, producing output that no longer round-trips:
The
,is now part of the# commentline, soaddedhas no separator and re-parsing the result raises aParseError.Cause
In
InlineTable.as_string, the separator for a newly-appended key is inserted lazily when the next key is reached:buf.rstrip()strips trailing whitespace, but when the previous value is followed by a comment it lands after the comment text rather than after the value, so the comma is swallowed by the comment.Fix
Track the buffer position right after each rendered value (
last_value_end) and splice the deferred separator there. When there is no trailing comment this is exactly wherebuf.rstrip()pointed, so existing behaviour is unchanged; it only differs in the comment case. Output now round-trips:Tests
Added
test_appending_to_inline_table_with_trailing_comment_keeps_comma_outside_comment(asserts the comma is outside the comment and that the result round-trips). It fails onmasterand passes with the fix; the full suite (993 tests) stays green.