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 · 5 comments · Fixed by #3822
Closed

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

JelleZijlstra opened this issue Jun 23, 2023 · 5 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.

4 participants