-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
ast.unparse
is needlessly "hip" for f-string quotes in Python 3.12+
#127975
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
Comments
One one's hand I would say "3.11 is oldstable so we shouldn't really care", but on the other hand, I don't think it should be that much work on our side to make it compatible. I can see some value for static analysis tools that would benefit from this. |
I think alternating quotes is better stylistically so it would be preferable to use the old style, but I agree with Pablo that we should do this only if it doesn't unduly complicate the implementation. |
PR here: #127980 The second bug Pablo links to is not really implementation issue, is just that code predated PEP 701, but agree in general cosmetic unparsing gets finnicky. In particular, I think we should not reinstate the old code that avoided using backslashes. i.e.
|
What will be the intended result of this? Should we use triple quotes? >>> print(ast.unparse(ast.parse("""f\"""{f'''{f"{''}"}'''}""\"""")))
f'''{f"""{f"{''}"}"""}'''
>>> """f'''{f"{''}"}'''"""
'f\'\'\'{f"{\'\'}"}\'\'\'' |
…ed (pythonGH-127980) (cherry picked from commit 8df5193) Co-authored-by: Shantanu <[email protected]>
…ed (pythonGH-127980) (cherry picked from commit 8df5193) Co-authored-by: Shantanu <[email protected]>
Feature or enhancement
Proposal:
Up until Python 3.11, the output of the following
ast.parse
/unparse
runwas
In Python 3.12, new f-string features were introduced, allowing for more general quote combinations. The output of the above script in Python 3.12 and 3.13 is
While this is legal Python 3.12/3.13, this representation needlessly restricts the usability of the generated code: It will not work on Python 3.11 and earlier.
I would thus like to suggest for
ast.unparse
to return, if possible, the more compatible code; in this casewhich works across all currently supported Python versions.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
The text was updated successfully, but these errors were encountered: