New submission from Ilya Kamenshchikov <[email protected]>:
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):
print(
type(n.format_spec),
len(n.format_spec.values),
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 <[email protected]>
<https://bugs.python.org/issue39760>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com