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

Avoid raising UP032 if format call arguments contain multiline expressions #5971

Merged
merged 3 commits into from
Jul 22, 2023
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
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}"