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

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

Closed
Hnasar opened this issue Apr 3, 2024 · 1 comment · Fixed by #10766
Closed

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

Hnasar opened this issue Apr 3, 2024 · 1 comment · Fixed by #10766
Assignees
Labels
bug Something isn't working

Comments

@Hnasar
Copy link
Contributor

Hnasar commented Apr 3, 2024

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
@charliermarsh charliermarsh added the bug Something isn't working label Apr 3, 2024
@dhruvmanila dhruvmanila self-assigned this Apr 4, 2024
@dhruvmanila
Copy link
Member

Oops, sorry. I think I have a permanent fix for this.

Glyphack pushed a commit to Glyphack/ruff that referenced this issue Apr 12, 2024
…l-sh#10766)

## Summary

This PR adds a new semantic model flag to indicate that the checker is
inside an f-string replacement field. This will be used to ignore
certain checks if the target version doesn't support a specific feature
like PEP 701.

fixes: astral-sh#10761 

## Test Plan

Add a test case from the raised issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants