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

False positive f-string SyntaxError #7778

Closed
youknowone opened this issue Oct 3, 2023 · 1 comment · Fixed by #7780
Closed

False positive f-string SyntaxError #7778

youknowone opened this issue Oct 3, 2023 · 1 comment · Fixed by #7780
Assignees
Labels
bug Something isn't working parser Related to the parser

Comments

@youknowone
Copy link
Contributor

# x.py
f"{1:{{1}.pop()}{f'n'}}"
$ ruff x.py
error: Failed to parse x.py:1:16: f-string: single '}' is not allowed
x.py:1:16: E999 SyntaxError: f-string: single '}' is not allowed
Found 1 error.
$ ruff --version
ruff 0.0.292
@dhruvmanila
Copy link
Member

Thanks for the bug report! It seems to be caused by the two {{ inside the format spec part. It seems that the rules for escaping {/} are different in and out of a format spec.

@dhruvmanila dhruvmanila added bug Something isn't working parser Related to the parser labels Oct 3, 2023
@dhruvmanila dhruvmanila self-assigned this Oct 3, 2023
dhruvmanila added a commit that referenced this issue Oct 3, 2023
## Summary

This PR fixes a bug in the lexer for f-string format spec where it would
consider the `{{` (double curly braces) as an escape pattern.

This is not the case as evident by the
[PEP](https://peps.python.org/pep-0701/#how-to-produce-these-new-tokens)
as well but I missed the part:

> [..]
> * **If in “format specifier mode” (see step 3), an opening brace ({)
or a closing brace (}).**
> * If not in “format specifier mode” (see step 3), an opening brace ({)
or a closing brace (}) that is not immediately followed by another
opening/closing brace.

## Test Plan

Add a test case to verify the fix and update the snapshot.

fixes: #7778
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser Related to the parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants