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

Support PEP 701: Syntactic formalization of f-strings in 3.12 #3746

Closed
JelleZijlstra opened this issue Jun 23, 2023 · 8 comments · Fixed by #3822
Closed

Support PEP 701: Syntactic formalization of f-strings in 3.12 #3746

JelleZijlstra opened this issue Jun 23, 2023 · 8 comments · Fixed by #3822
Labels
C: parser How we parse code. Or fail to parse it. T: enhancement New feature or request

Comments

@JelleZijlstra
Copy link
Collaborator

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:

>>> f"{f"{"f"}"}"
'f'

We should support this new syntax, somehow.

I don't know if this is doable with blib2to3.

@JelleZijlstra JelleZijlstra added T: enhancement New feature or request C: parser How we parse code. Or fail to parse it. labels Jun 23, 2023
@zero00072
Copy link

zero00072 commented Sep 15, 2023

I have same problem.

My Python code:

#!/usr/bin/env python3.12

print(f"The result: {"1" if True else "0"}")

black it:

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=[],
)

@DarkAlchy
Copy link

Apparently, this was never fixed as I have the same issue with the web demo or ran locally.

python --version
Python 3.10.12

@sodul
Copy link

sodul commented Jan 5, 2024

@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 := operator introduced with 3.8.

@JelleZijlstra
Copy link
Collaborator Author

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.

@sodul
Copy link

sodul commented Jan 5, 2024

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!

@tomfrost435
Copy link

Has this issue been resolved?
I'm currently getting the same error when the formatter tries to parse a ternary operator inside an f-string, the issue isnt that it cant format that specific line but more that it refuses to format the rest of the file because of it.

Cannot parse: 37:32: print(f"These two strings {"are" if are_anagrams(word1, word2) else "are not"} anagrams")

Oh no! 💥 💔 💥
1 file failed to reformat.

@tvuotila
Copy link

@tomfrost435 This issue has been resolved. I suggest you to create a new issue https://github.com/psf/black/issues/new/choose

@JelleZijlstra
Copy link
Collaborator Author

You are likely running an old version of Black or running Black under a version of Python prior to 3.12.

@psf psf locked as resolved and limited conversation to collaborators Aug 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C: parser How we parse code. Or fail to parse it. T: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants