berkerpeksag / astor

Python AST read/write
https://pypi.org/project/astor/
BSD 3-Clause "New" or "Revised" License
803 stars 102 forks source link

New fstring failures #124

Closed felixonmars closed 5 years ago

felixonmars commented 5 years ago

I'm not sure how to handle this one, so I did not submit a PR yet.

Source (from Python 3.7's dataclasses.py): f"{f.name}={{self.{f.name}!r}}"

Parsed as:

JoinedStr(
    values=[
        FormattedValue(value=Attribute(value=Name(id='f'), attr='name'), conversion=-1, format_spec=None),
        Str(s='={self.'),
        FormattedValue(value=Attribute(value=Name(id='f'), attr='name'), conversion=-1, format_spec=None),
        Str(s='!r}')]),

which becomes a syntax error after rtrip: f'{f.name}={self.{f.name}!r}'

  File "<fstring>", line 1
    (self.{f.name})
          ^
SyntaxError: invalid syntax