Skip to content
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 @@ -269,3 +269,10 @@ async def c():
# Not a valid type annotation but this test shouldn't result in a panic.
# Refer: https://github.com/astral-sh/ruff/issues/11736
x: "'{} + {}'.format(x, y)"

# Regression https://github.com/astral-sh/ruff/issues/21000
# Fix should parenthesize walrus
if __name__ == "__main__":
number = 0
string = "{}".format(number := number + 1)
print(string)
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ fn parenthesize(expr: &Expr, text: &str, context: FormatContext) -> bool {
Expr::BinOp(_)
| Expr::UnaryOp(_)
| Expr::BoolOp(_)
| Expr::Named(_)
| Expr::Compare(_)
| Expr::If(_)
| Expr::Lambda(_)
Expand All @@ -167,6 +166,7 @@ fn parenthesize(expr: &Expr, text: &str, context: FormatContext) -> bool {
_,
Expr::Generator(_)
| Expr::Dict(_)
| Expr::Named(_)
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice if we could use OperatorPrecedence here (I believe that's the name) instead of listing all expressions

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hm... it's not immediately clear how to do this here - especially because some of this logic is to avoid double escaped braces for sets and dicts, like f"{{x}}"

| Expr::Set(_)
| Expr::SetComp(_)
| Expr::DictComp(_),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1320,10 +1320,32 @@ UP032 [*] Use f-string instead of `format` call
270 | # Refer: https://github.com/astral-sh/ruff/issues/11736
271 | x: "'{} + {}'.format(x, y)"
| ^^^^^^^^^^^^^^^^^^^^^^
272 |
273 | # Regression https://github.com/astral-sh/ruff/issues/21000
|
help: Convert to f-string
268 |
269 | # Not a valid type annotation but this test shouldn't result in a panic.
270 | # Refer: https://github.com/astral-sh/ruff/issues/11736
- x: "'{} + {}'.format(x, y)"
271 + x: "f'{x} + {y}'"
272 |
273 | # Regression https://github.com/astral-sh/ruff/issues/21000
274 | # Fix should parenthesize walrus

UP032 [*] Use f-string instead of `format` call
--> UP032_0.py:277:14
|
275 | if __name__ == "__main__":
276 | number = 0
277 | string = "{}".format(number := number + 1)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
278 | print(string)
|
help: Convert to f-string
274 | # Fix should parenthesize walrus
275 | if __name__ == "__main__":
276 | number = 0
- string = "{}".format(number := number + 1)
277 + string = f"{(number := number + 1)}"
278 | print(string)