-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Support PEP 701: Syntactic formalization of f-strings in 3.12 #3746
Comments
I have same problem. My Python code: #!/usr/bin/env python3.12
print(f"The result: {"1" if True else "0"}")
user@localhost:~$ black test.py
error: cannot format test.py: Cannot parse: 3:22: print(f"The result: {"1" if True else "0"}")
Oh no! 💥 💔 💥
1 file failed to reformat. With Python console: >>> print(f"The result: {"1" if True else "0"}")
The result: 1 The AST: import ast
_ = ast.Module(
[
ast.Expr(
ast.Call(
ast.Name("print"),
[
ast.JoinedStr(
[
ast.Constant("The result: "),
ast.FormattedValue(
ast.IfExp(
ast.Constant(True),
ast.Constant("1"),
ast.Constant("0"),
),
-1,
),
]
)
],
[],
)
)
],
type_ignores=[],
) |
Apparently, this was never fixed as I have the same issue with the web demo or ran locally. python --version |
@hauntsaninja do you have an update on this issue and if there is work to get it resolved? I see that it seems to be a common problem which has new reports popping up every few weeks. I do understand that this is probably tricky to fix but it is pretty bad that black is unable to parse what is now valid python. Our workaround is to extract variables out of f-strings so that there are no parsing issues, but that defeats the purpose of PEP 701. I know the new syntax is not valid with 3.11 or lower but the same could have been said of the |
There is an open PR, #3822, but it hasn't made much progress recently. Perhaps you can work with the PR author to move it along. This grammar change is much trickier than most previous ones because it involves changes to the tokenizer. That's why it's been harder to get into Black than most previous grammar changes. |
Thanks for the update @JelleZijlstra, I've worked with 'quoting' libraries several lifetimes ago, so I fully appreciate how difficult supporting PEP 701 can be and I'm afraid I would not be much help here, except for moral support. Maybe whomever implemented PEP 701 for 3.12 could help? Thank you for your work on this! |
Has this issue been resolved? Cannot parse: 37:32: print(f"These two strings {"are" if are_anagrams(word1, word2) else "are not"} anagrams") Oh no! 💥 💔 💥 |
@tomfrost435 This issue has been resolved. I suggest you to create a new issue https://github.com/psf/black/issues/new/choose |
You are likely running an old version of Black or running Black under a version of Python prior to 3.12. |
https://peps.python.org/pep-0701/, which will be in Python 3.12, adds support for several f-string variations that our current parser won't handle:
We should support this new syntax, somehow.
I don't know if this is doable with blib2to3.
The text was updated successfully, but these errors were encountered: