Repository navigation
fix: honour escape=False for a leading newline in multiline strings - #656
Open
shivampwm2020 wants to merge 1 commit into
Open
shivampwm2020 wants to merge 1 commit into
shivampwm2020 wants to merge 1 commit into
Conversation
PR python-poetry#551 re-added the newline trimmed by the parser for any multiline string whose value starts with one, but did so regardless of the escape flag. With escape=False the caller owns the raw body, so the extra newline made it impossible to render such a value as written. Gate the compensation on escape so the raw body is emitted verbatim when escaping is disabled, restoring the pre-python-poetry#551 behaviour for that case. Fixes python-poetry#655
Wkhanex
reviewed
Oct 14, 2026
Wkhanex
left a comment
There was a problem hiding this comment.
With escape=False, multiline basic and literal strings retain the exact supplied raw body, including LF and CRLF at the start. Default escaping still preserves the original parsed value, so the raw-body fix does not remove the protective newline on the ordinary path.
Checked 739d8bc8481d with standalone Python 3.12.14 checks (12 passing assertions). I haven't run the full upstream suite.
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.
Problem
tomlkit.string(..., escape=False)can no longer render a multiline string whose value starts with a newline. The value is correct in memory but the serialized document gains a spurious leading newline:Clean
master:The extra blank line after the opening
\'\'\'means the intended output\'\'\'\n aligned\n strings\n\'\'\'cannot be produced at all - matching the report in #655.Root cause
tomlkit/items.py,String.from_raw(~line 2314):This re-adds the newline that the parser trims immediately after a multiline opening delimiter, so a leading newline survives a round-trip. It was introduced unconditionally by PR #551 ("fix: preserve leading newline of multiline string built with
string()", commit8cd44f5).The compensation only makes sense when
escape=True. Withescape=Falsethe caller explicitly owns the raw body (seetomlkit/api.py:137-153), so prepending a newline corrupts it. Before #551 theescape=Falsecase rendered verbatim.Fix
Gate the compensation on
escape:escape=Truekeeps #551's round-trip preservation (the existingtest_create_multiline_string_with_leading_newline_round_tripsstill passes);escape=Falsenow emits the body exactly as given.Testing
Regression tests added to
tests/test_api.py:test_create_multiline_literal_string_with_leading_newline_no_escapetest_create_multiline_basic_string_with_leading_newline_no_escapeRED (test only, fix reverted):
GREEN (with fix):
Full suite on the fixed tree:
1204 passed in 6.00s.ruff check tomlkit/items.py tests/test_api.py->All checks passed!Fixes #655