Skip to content

SyntaxError after bad fix in f-string from Q000 flake8 bad-quotes-inline-string #10761

@Hnasar

Description

@Hnasar

Using ruff 0.3.5: the fix for https://docs.astral.sh/ruff/rules/bad-quotes-inline-string/ results in a syntax error

# foo.py
x = y = z = 5
s = f"Before {f'x {x}' if y else f'foo {z}'} after"
$ ruff check --target-version=py310 --select=Q foo.py  
foo.py:2:34: Q000 [*] Single quotes found but double quotes preferred
--- foo.py
+++ foo.py
@@ -1,2 +1,2 @@
 x = y = z = 5
-s = f"Before {f'x {x}' if y else f'foo {z}'} after"
+s = f"Before {f'x {x}' if y else f"foo {z}"} after"

After applying --fix

$ python3.10 foo.py 
  File "foo.py", line 2
    s = f"Before {f'x {x}' if y else f"foo {z}"} after"
                                       ^^^
SyntaxError: f-string: expecting '}'

Notes:

  • The "fixed" syntax is only valid in 3.12 because it supports arbitrarily nested fstrings
  • I cannot reproduce this bad fix using ruff 0.1.6

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions