Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not consider f-strings with escaped newlines as multiline #14624

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,10 @@
expression } bbbbbbbbbbbbbbbbbbbbbbbb" + (
yyyyyyyyyyyyyy + zzzzzzzzzzz
)

# This is not a multiline f-string, but the expression is too long so it should be
# wrapped in parentheses.
f"hellooooooooooooooooooooooo \
Copy link
Member

Choose a reason for hiding this comment

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

Could we add some tests in assignment positions and with implicit concatenated strings (especially with inline comments) just to make sure strings with line continuations are handled correctly in those positions too

Copy link
Member Author

Choose a reason for hiding this comment

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

Adding these test case reveals one more change in implicitly concatenated strings. Given:

f"hellooooooooooooooooooooooo \
        worldddddddddddddddddd" "another string"

On main, we don't concatenate it because the f-string is considered as multiline but here we would. I'm going to make the is_multiline change gated behind preview first, rebase this PR and add the test cases.

Copy link
Member

Choose a reason for hiding this comment

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

I think concatenating the string makes sense as long as it fits into the line length

worlddddddddddddddddddddddddddddddddd" + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
aaaaaaaaaaa = f"hellooooooooooooooooooooooo \
worlddddddddddddddddddddddddddddddddd" + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,24 @@ def implicit_with_comment():
"a" f'{1=: "abcd \'\'}'
f'{1=: "abcd \'\'}' "a"
f'{1=: "abcd \'\'}' f"{1=: 'abcd \"\"}"

# These strings contains escaped newline characters and should be joined, they are
# not multiline strings.
f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb" "cccccccccccccc \
ddddddddddddddddddd"
b"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb" b"cccccccccccccc \
ddddddddddddddddddd"
f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb" "cccccccccccccc \
ddddddddddddddddddd" # comment 1
(f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb" "cccccccccccccc \
ddddddddddddddddddd") # comment 2
(
f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb" # comment 3
"cccccccccccccc \
ddddddddddddddddddd" # comment 4
)
16 changes: 13 additions & 3 deletions crates/ruff_python_formatter/src/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ impl StringLikeExtensions for ast::StringLike<'_> {
fn contains_line_break_or_comments(
elements: &ast::FStringElements,
context: &PyFormatContext,
is_triple_quoted: bool,
) -> bool {
elements.iter().any(|element| match element {
ast::FStringElement::Literal(literal) => {
context.source().contains_line_break(literal.range())
is_triple_quoted
&& context.source().contains_line_break(literal.range())
}
ast::FStringElement::Expression(expression) => {
// Expressions containing comments can't be joined.
Expand All @@ -132,7 +134,11 @@ impl StringLikeExtensions for ast::StringLike<'_> {
// ```
context.comments().contains_comments(expression.into())
|| expression.format_spec.as_deref().is_some_and(|spec| {
contains_line_break_or_comments(&spec.elements, context)
contains_line_break_or_comments(
&spec.elements,
context,
is_triple_quoted,
)
})
|| expression.debug_text.as_ref().is_some_and(|debug_text| {
memchr2(b'\n', b'\r', debug_text.leading.as_bytes()).is_some()
Expand All @@ -144,7 +150,11 @@ impl StringLikeExtensions for ast::StringLike<'_> {
}

if is_f_string_formatting_enabled(context) {
contains_line_break_or_comments(&f_string.elements, context)
contains_line_break_or_comments(
&f_string.elements,
context,
f_string.flags.is_triple_quoted(),
)
} else {
context.source().contains_line_break(f_string.range())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,13 @@ xxxxxxxxxxxxxxxx = f"aaaaaaaaaaaaaaaaaaaaa {
expression } bbbbbbbbbbbbbbbbbbbbbbbb" + (
yyyyyyyyyyyyyy + zzzzzzzzzzz
)

# This is not a multiline f-string, but the expression is too long so it should be
# wrapped in parentheses.
f"hellooooooooooooooooooooooo \
worlddddddddddddddddddddddddddddddddd" + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
aaaaaaaaaaa = f"hellooooooooooooooooooooooo \
worlddddddddddddddddddddddddddddddddd" + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
```

## Output
Expand Down Expand Up @@ -906,14 +913,25 @@ if True:
# This f-string should be flattened
xxxxxxxxxxxxxxxx = f"aaaaaaaaaaaaaaaaaaaaa {
expression } bbbbbbbbbbbbbbbbbbbbbbbb" + (yyyyyyyyyyyyyy + zzzzzzzzzzz)

# This is not a multiline f-string, but the expression is too long so it should be
# wrapped in parentheses.
f"hellooooooooooooooooooooooo \
worlddddddddddddddddddddddddddddddddd" + (
aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb
)
aaaaaaaaaaa = f"hellooooooooooooooooooooooo \
worlddddddddddddddddddddddddddddddddd" + (
aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb
)
```


## Preview changes
```diff
--- Stable
+++ Preview
@@ -468,5 +468,6 @@
@@ -468,16 +468,19 @@
)

# This f-string should be flattened
Expand All @@ -922,4 +940,23 @@ xxxxxxxxxxxxxxxx = f"aaaaaaaaaaaaaaaaaaaaa {
+xxxxxxxxxxxxxxxx = f"aaaaaaaaaaaaaaaaaaaaa {expression} bbbbbbbbbbbbbbbbbbbbbbbb" + (
+ yyyyyyyyyyyyyy + zzzzzzzzzzz
+)

# This is not a multiline f-string, but the expression is too long so it should be
# wrapped in parentheses.
-f"hellooooooooooooooooooooooo \
- worlddddddddddddddddddddddddddddddddd" + (
- aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb
+(
+ f"hellooooooooooooooooooooooo \
+ worlddddddddddddddddddddddddddddddddd"
+ + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
)
-aaaaaaaaaaa = f"hellooooooooooooooooooooooo \
- worlddddddddddddddddddddddddddddddddd" + (
- aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb
+aaaaaaaaaaa = (
+ f"hellooooooooooooooooooooooo \
+ worlddddddddddddddddddddddddddddddddd"
+ + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
)
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/join_implicit_concatenated_string.py
snapshot_kind: text
---
## Input
```python
Expand Down Expand Up @@ -333,6 +332,27 @@ assert False, +"Implicit concatenated string" "uses {} layout on {} format".form
"a" f'{1=: "abcd \'\'}'
f'{1=: "abcd \'\'}' "a"
f'{1=: "abcd \'\'}' f"{1=: 'abcd \"\"}"

# These strings contains escaped newline characters and should be joined, they are
# not multiline strings.
f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb" "cccccccccccccc \
ddddddddddddddddddd"
b"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb" b"cccccccccccccc \
ddddddddddddddddddd"
f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb" "cccccccccccccc \
ddddddddddddddddddd" # comment 1
(f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb" "cccccccccccccc \
ddddddddddddddddddd") # comment 2
(
f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb" # comment 3
"cccccccccccccc \
ddddddddddddddddddd" # comment 4
)
```

## Outputs
Expand Down Expand Up @@ -747,4 +767,28 @@ assert False, +"Implicit concatenated stringuses {} layout on {} format".format(
f'a{1=: "abcd \'\'}'
f'{1=: "abcd \'\'}a'
f'{1=: "abcd \'\'}' f"{1=: 'abcd \"\"}"

# These strings contains escaped newline characters and should be joined, they are
# not multiline strings.
f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbbcccccccccccccc \
ddddddddddddddddddd"
b"aaaaaaaaaaaaaaaa \
bbbbbbbbbbbcccccccccccccc \
ddddddddddddddddddd"
f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbbcccccccccccccc \
ddddddddddddddddddd" # comment 1
(
f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb"
"cccccccccccccc \
ddddddddddddddddddd"
) # comment 2
(
f"aaaaaaaaaaaaaaaa \
bbbbbbbbbbb" # comment 3
"cccccccccccccc \
ddddddddddddddddddd" # comment 4
)
```