Reject multi-line f-string elements before Python 3.12#24355
Reject multi-line f-string elements before Python 3.12#24355charliermarsh merged 2 commits intomainfrom
Conversation
|
4441c92 to
6b9a7e9
Compare
crates/ruff_python_formatter/tests/snapshots/format@expression__fstring.py.snap
Show resolved
Hide resolved
...s/ruff_python_formatter/tests/snapshots/format@expression__nested_string_quote_style.py.snap
Show resolved
Hide resolved
bf9607e to
0cb5556
Compare
0cb5556 to
03d8338
Compare
## 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).
03d8338 to
da1acf5
Compare
| let source = self.source[range].as_bytes(); | ||
| let has_line_break = |
There was a problem hiding this comment.
Given that this is a non-trivial amount of work, I suggest we gate the checks with an early "Is this <= 3.12?" check.
There was a problem hiding this comment.
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<_>>(); |
There was a problem hiding this comment.
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.
130ce44 to
3144fb7
Compare
3144fb7 to
d2cd4d1
Compare
## 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.
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.