[Python-Dev] ast changes for "debug" f-strings

Serhiy Storchaka storchaka at gmail.com
Mon May 20 13:13:31 EDT 2019

20.05.19 16:25, Eric V. Smith пише:
> I added an optional "expr_text" field to the FormattedValue node, which 
> represents the text of the expression in a "debug" f-string expression. 
> So in f'{x=}', expr_text would be "x=".
> This strictly speaking isn't necessary. I could have added another 
> Constant node for "x=" and left FormattedValue alone. I didn't for three 
> reasons: it was expedient; it didn't require a lot of surgery to 
> f-string parsing, which the extra Constant node would require; and it 
> allowed the Python/ast_unparse.c code to produce a string that was more 
> consistent with input string.
> Now that I have more time, I'm rethinking this decision. I'd rather not 
> have the extra churn that modifying the ast node will cause to 
> downstream code. Someone (Serhiy?) already pointed this out somewhere, 
> but now I can't find the original message.

Of course I am for removing "expr_text". This makes the AST simpler. I 
do not care about ast_unparse because it does nor roundtrip in general. 
For example it can add and remove parenthesis, commas, backslashes. 
Currently it even has a bug in handling some corner cases in f-strings 
-- nobody complains.

In theory it is possible to detect when !d was used (parse the previous 
Constant value and compare the result with the FormattedValue 
expression). We can implement this if there is a need after beta1.

More information about the Python-Dev mailing list