Repository navigation
Closed
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/ericvsmith'
closed_at = <Date 2020-11-20.21:53:16.672>
created_at = <Date 2016-09-07.17:18:18.601>
labels = ['type-bug', 'library', '3.9']
title = "ast.unparse can't roundtrip some f-strings"
updated_at = <Date 2020-11-20.21:55:43.133>
user = 'https://github.com/ericvsmith'bugs.python.org fields:
activity = <Date 2020-11-20.21:55:43.133>
actor = 'eric.smith'
assignee = 'eric.smith'
closed = True
closed_date = <Date 2020-11-20.21:53:16.672>
closer = 'BTaskaya'
components = ['Library (Lib)']
creation = <Date 2016-09-07.17:18:18.601>
creator = 'eric.smith'
dependencies = []
files = []
hgrepos = []
issue_num = 28002
keywords = ['patch']
message_count = 12.0
messages = ['274841', '341502', '341512', '341633', '366481', '366796', '366800', '374724', '378530', '381507', '381509', '381510']
nosy_count = 10.0
nosy_names = ['simon.percivall', 'eric.smith', 'berker.peksag', 'serhiy.storchaka', 'pablogsal', 'miss-islington', 'remi.lapeyre', 'BTaskaya', 'loganasherjones', 'hauntsaninja']
pr_nums = ['19612', '23430']
priority = 'low'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue28002'
versions = ['Python 3.9']
Activity
ericvsmith commentedon Sep 7, 2016
The problem relates to how f-strings are re-created.
For the input file foo.txt containing this one line:
f'''{"'"}'''
Run:
./python Tools/parser/test_unparse.py foo.txt
Gives this output:
f'{"\'"}'
This result is not a valid f-string, since it contains a backslash inside the expression part of the f-string.
[-]f-strings do not round trip through Tools/parser/test_unparse.py[/-][+]Some f-strings do not round trip through Tools/parser/test_unparse.py[/+]serhiy-storchaka commentedon May 6, 2019
There is the same problem with annotations.
You can look at https://github.com/berkerpeksag/astor. Maybe this problem is solved in that project.
remilapeyre commentedon May 6, 2019
The issue is also present in Astor:
The issue comes too from calling repr to format the inner string as it does not try to minimize the number of \.
loganasherjones commentedon May 6, 2019
After speaking with Lukasz about this, it seems like the unparser is using the normal unicode repr to determine what should be returned. The default unicode repr will escape quotes if necessary. This is not allowed for f-strings and is the root cause of the problem.
One way to solve this is to add a flag to the unicode_repr function to determine whether or not we need to allow triple quotes in the output of the repr. By default this will be false and will use backslashes, but the ast_unparse will use true for this.
[-]Some f-strings do not round trip through Tools/parser/test_unparse.py[/-][+]ast.unparse can't roundtrip some f-strings[/+]hauntsaninja commentedon Apr 15, 2020
Now that
ast.unparseis being added to stdlib, I thought I'd bump this thread. The third party libraryastunparse(which attempts to support multiple Python versions while staying very close to Tools/parser/unparse.py) has an implementation that works for complex cases.If that sounds good, I can submit a PR and inform the original author (astunparse is license is based on PSF's, so should be okay if the original author doesn't respond).
https://github.com/simonpercivall/astunparse/blob/master/lib/astunparse/unparser.py#L461
isidentical commentedon Apr 19, 2020
I can't say anything before seeing the approach but fixing the problem definitely sounds good.
simonpercivall commentedon Apr 19, 2020
Any and all code from astunparse is certainly available for inclusion. Go ahead.
hauntsaninja commentedon Aug 3, 2020
Just bumping this issue, as per dev guide, since #19612 has been ready for about two months. Would be grateful for review :-)
hauntsaninja commentedon Oct 12, 2020
Bumping again!
isidentical commentedon Nov 20, 2020
New changeset a993e90 by Shantanu in branch 'master':
bpo-28002: Roundtrip f-strings with ast.unparse better (bpo-19612)
a993e90
isidentical commentedon Nov 20, 2020
New changeset 3763cc1 by Miss Islington (bot) in branch '3.9':
bpo-28002: Roundtrip f-strings with ast.unparse better (GH-19612) (GH-23430)
3763cc1
isidentical commentedon Nov 20, 2020
Thanks for your efforts Shantanu!