Skip to content

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

Closed
nschloe opened this issue Dec 15, 2024 · 5 comments
Closed

ast.unparse is needlessly "hip" for f-string quotes in Python 3.12+ #127975

nschloe opened this issue Dec 15, 2024 · 5 comments
Labels
stdlib Python modules in the Lib dir topic-parser type-feature A feature request or enhancement

Comments

@nschloe
Copy link

nschloe commented Dec 15, 2024

Feature or enhancement

Proposal:

Up until Python 3.11, the output of the following ast.parse/unparse run

import ast
from sys import version_info as vi

print(f"{vi.major}.{vi.minor}.{vi.micro}")
print(ast.unparse(ast.parse("f\"{'.' * 5}\"")))

was

3.11.11
f"{'.' * 5}"

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

3.12.7
f'{'.' * 5}'

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 case

f"{'.' * 5}"

which 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

@nschloe nschloe added the type-feature A feature request or enhancement label Dec 15, 2024
@tomasr8 tomasr8 added stdlib Python modules in the Lib dir topic-parser labels Dec 15, 2024
@picnixz
Copy link
Member

picnixz commented Dec 15, 2024

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.

cc @pablogsal @JelleZijlstra

@pablogsal
Copy link
Member

I agree but the challenge here is that the unparse code is very sensitive to many of nodes and is very easy to fall into bugs such as #108843 or #108469 so it's a matter of checking if the fix is worth the maintenance cost.

@nschloe do you want to propose a PR so we can evaluate it?

@JelleZijlstra
Copy link
Member

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.

@hauntsaninja
Copy link
Contributor

hauntsaninja commented Dec 16, 2024

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.

# Python 3.11 behaviour
>>> print(ast.unparse(ast.parse("""f'''{\"""\n\"""}'''""")))
f'''{"""
"""}'''

# Python 3.12 behaviour
>>> print(ast.unparse(ast.parse("""f'''{\"""\n\"""}'''""")))
f'{'\n'}'

# Proposed behaviour in PR (still requires PEP 701)
>>> print(ast.unparse(ast.parse("""f'''{\"""\n\"""}'''""")))
f"{'\n'}"

@nineteendo
Copy link
Contributor

What will be the intended result of this? Should we use triple quotes?

>>> print(ast.unparse(ast.parse("""f\"""{f'''{f"{''}"}'''}""\"""")))
f'''{f"""{f"{''}"}"""}'''

repr(string) doesn't use them (even though it could):

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

hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue Jan 31, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 2, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 2, 2025
hauntsaninja added a commit that referenced this issue Feb 2, 2025
…ded (GH-127980) (#129601)

gh-127975: Avoid reusing quote types in ast.unparse if not needed (GH-127980)
(cherry picked from commit 8df5193)

Co-authored-by: Shantanu <[email protected]>
hauntsaninja added a commit that referenced this issue Feb 3, 2025
…ded (GH-127980) (#129600)

gh-127975: Avoid reusing quote types in ast.unparse if not needed (GH-127980)
(cherry picked from commit 8df5193)

Co-authored-by: Shantanu <[email protected]>
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-parser type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

7 participants