Skip to content

Reject multi-line f-string elements before Python 3.12#24355

Merged
charliermarsh merged 2 commits intomainfrom
charlie/parser-multiline
Apr 5, 2026
Merged

Reject multi-line f-string elements before Python 3.12#24355
charliermarsh merged 2 commits intomainfrom
charlie/parser-multiline

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

Summary

Before Python 3.12, a replacement field in an f-string can span multiple lines only if the outer f-string is triple-quoted. This was relaxed in Python 3.12, but we weren't rejecting these as syntactically invalid on earlier versions.

Closes #24348.

@charliermarsh charliermarsh added the parser Related to the parser label Apr 2, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 2, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@charliermarsh charliermarsh force-pushed the charlie/parser-multiline branch from 4441c92 to 6b9a7e9 Compare April 2, 2026 01:19
@charliermarsh charliermarsh marked this pull request as ready for review April 2, 2026 01:56
@astral-sh-bot astral-sh-bot bot requested a review from ntBre April 2, 2026 01:56
@charliermarsh charliermarsh force-pushed the charlie/parser-multiline branch from bf9607e to 0cb5556 Compare April 2, 2026 20:49
@charliermarsh charliermarsh changed the base branch from main to charlie/det April 2, 2026 20:49
@charliermarsh charliermarsh force-pushed the charlie/parser-multiline branch from 0cb5556 to 03d8338 Compare April 2, 2026 20:57
charliermarsh added a commit that referenced this pull request Apr 3, 2026
## Summary

Right now these tests are dependent on input order, so changes in the
underlying hash can lead to churn in the fixtures. See, e.g.:
#24355 (comment).
Base automatically changed from charlie/det to main April 3, 2026 13:59
@charliermarsh charliermarsh force-pushed the charlie/parser-multiline branch from 03d8338 to da1acf5 Compare April 3, 2026 14:00
Copy link
Copy Markdown
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

Comment on lines +1915 to +1916
let source = self.source[range].as_bytes();
let has_line_break =
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is a non-trivial amount of work, I suggest we gate the checks with an early "Is this <= 3.12?" check.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole block is already gated by if !self.options.target_version.supports_pep_701() (which is "Is this <= 3.12?").

let slash_position = TextSize::try_from(slash_position).unwrap();
TextRange::at(range.start() + slash_position, '\\'.text_len())
})
.collect::<Vec<_>>();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use peekable here and for comment_ranges instead of collecting or even better, move the backslash and comment emitting code above the has_line_break check and use a single has_backslash_or_comment boolean that you set when there's any comment or backslash.

@charliermarsh charliermarsh force-pushed the charlie/parser-multiline branch 2 times, most recently from 130ce44 to 3144fb7 Compare April 5, 2026 15:00
@charliermarsh charliermarsh force-pushed the charlie/parser-multiline branch from 3144fb7 to d2cd4d1 Compare April 5, 2026 15:04
@charliermarsh charliermarsh enabled auto-merge (squash) April 5, 2026 15:05
@charliermarsh charliermarsh merged commit 9a55bc6 into main Apr 5, 2026
48 checks passed
@charliermarsh charliermarsh deleted the charlie/parser-multiline branch April 5, 2026 15:09
charliermarsh added a commit that referenced this pull request Apr 5, 2026
## Summary

See:
#24355 (comment).
Prior to Python 3.12, we need to avoid emitting formatted expressions
that span multiple lines in non-triple quoted f-strings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ruff fails to detect python 3.10 unterminated string literal syntax error

3 participants