[New-bugs-announce] [issue39760] ast.FormattedValue.format_spec unnecessarily wrapped in JoinedStr

Ilya Kamenshchikov report at bugs.python.org
Wed Feb 26 07:43:02 EST 2020

New submission from Ilya Kamenshchikov <ikamenshchikov at gmail.com>:

Most usual usecase for format_spec is to specify it as a constant, that would be logical to represent as ast.Constant. However, ast.parse wraps value of ast.FormattedValue.format_spec into a JoinedStr with a single constant value, as can be seen from example below:

import ast

code = '''f"is {x:d}"'''
tree = ast.parse(code)

for n in ast.walk(tree):
    if isinstance(n, ast.FormattedValue):
            set(type(v) for v in n.format_spec.values),

This is confusing for programmatically analyzing the ast, and likely creates some overhead in any modules using ast and FormattedValue.

Proposal: represent ast.FormattedValue.format_spec as ast.Constant in most cases.

components: Library (Lib)
messages: 362691
nosy: Ilya Kamenshchikov
priority: normal
severity: normal
status: open
title: ast.FormattedValue.format_spec unnecessarily wrapped in JoinedStr
type: behavior
versions: Python 3.8

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list