`ast.unparse` is needlessly "hip" for f-string quotes in Python 3.12+ · Issue #127975 · python/cpython · GitHub
Skip to content

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

Description

@nschloe

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-parsertype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions