Skip to content

Commit

Permalink
Avoid raising UP032 if format call arguments contain multiline ex…
Browse files Browse the repository at this point in the history
…pressions (#5971)


## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

Fix a regression introduced by
#5638. A multiline expression
can't be safely inserted into a format field.

### Example

```
> cat a.py
"{}".format(
    [
        1,
        2,
        3,
    ]
)

> cargo run -p ruff_cli -- check a.py --no-cache --select UP032 --fix
    Finished dev [unoptimized + debuginfo] target(s) in 0.07s
     Running `target/debug/ruff check a.py --no-cache --select UP032 --fix`
error: Autofix introduced a syntax error in `a.py` with rule codes UP032: EOL while scanning string literal at byte offset 5
---
f"{[
        1,
        2,
        3,
    ]}"

---
a.py:1:1: UP032 Use f-string instead of `format` call
Found 1 error.
```


## Test Plan

New test cases
  • Loading branch information
harupy committed Jul 22, 2023
1 parent aba340a commit 050f595
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
16 changes: 16 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP032_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@
111111
)

"{}".format(
[
1,
2,
3,
]
)

"{a}".format(
a=[
1,
2,
3,
]
)

async def c():
return "{}".format(await 3)

Expand Down
8 changes: 6 additions & 2 deletions crates/ruff/src/rules/pyupgrade/rules/f_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ impl<'a> FormatSummaryValues<'a> {
let mut extracted_kwargs: FxHashMap<&str, &Expr> = FxHashMap::default();
if let Expr::Call(ast::ExprCall { args, keywords, .. }) = expr {
for arg in args {
if contains_invalids(locator.slice(arg.range())) {
if contains_invalids(locator.slice(arg.range()))
|| locator.contains_line_break(arg.range())
{
return None;
}
extracted_args.push(arg);
Expand All @@ -79,7 +81,9 @@ impl<'a> FormatSummaryValues<'a> {
range: _,
} = keyword;
if let Some(key) = arg {
if contains_invalids(locator.slice(value.range())) {
if contains_invalids(locator.slice(value.range()))
|| locator.contains_line_break(value.range())
{
return None;
}
extracted_kwargs.insert(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,54 +655,54 @@ UP032_0.py:69:85: UP032 [*] Use f-string instead of `format` call
75 73 | ###
76 74 | # Non-errors

UP032_0.py:136:11: UP032 [*] Use f-string instead of `format` call
UP032_0.py:152:11: UP032 [*] Use f-string instead of `format` call
|
135 | def d(osname, version, release):
136 | return"{}-{}.{}".format(osname, version, release)
151 | def d(osname, version, release):
152 | return"{}-{}.{}".format(osname, version, release)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP032
|
= help: Convert to f-string

Suggested fix
133 133 |
134 134 |
135 135 | def d(osname, version, release):
136 |- return"{}-{}.{}".format(osname, version, release)
136 |+ return f"{osname}-{version}.{release}"
137 137 |
138 138 |
139 139 | def e():
149 149 |
150 150 |
151 151 | def d(osname, version, release):
152 |- return"{}-{}.{}".format(osname, version, release)
152 |+ return f"{osname}-{version}.{release}"
153 153 |
154 154 |
155 155 | def e():

UP032_0.py:140:10: UP032 [*] Use f-string instead of `format` call
UP032_0.py:156:10: UP032 [*] Use f-string instead of `format` call
|
139 | def e():
140 | yield"{}".format(1)
155 | def e():
156 | yield"{}".format(1)
| ^^^^^^^^^^^^^^ UP032
|
= help: Convert to f-string

Suggested fix
137 137 |
138 138 |
139 139 | def e():
140 |- yield"{}".format(1)
140 |+ yield f"{1}"
141 141 |
142 142 |
143 143 | assert"{}".format(1)
153 153 |
154 154 |
155 155 | def e():
156 |- yield"{}".format(1)
156 |+ yield f"{1}"
157 157 |
158 158 |
159 159 | assert"{}".format(1)

UP032_0.py:143:7: UP032 [*] Use f-string instead of `format` call
UP032_0.py:159:7: UP032 [*] Use f-string instead of `format` call
|
143 | assert"{}".format(1)
159 | assert"{}".format(1)
| ^^^^^^^^^^^^^^ UP032
|
= help: Convert to f-string

Suggested fix
140 140 | yield"{}".format(1)
141 141 |
142 142 |
143 |-assert"{}".format(1)
143 |+assert f"{1}"
156 156 | yield"{}".format(1)
157 157 |
158 158 |
159 |-assert"{}".format(1)
159 |+assert f"{1}"


0 comments on commit 050f595

Please sign in to comment.