berkerpeksag / astor

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

ast.to_source does not return a valid source code (Call with f-string argument) #190

Open bombs-kim opened 3 years ago

bombs-kim commented 3 years ago

I found a case where ast.to_source does not return a valid python code. Here is an example.

from ast import parse
from astor import to_source

s1 = """len(f"'{''}'")"""
s2 = """len(f'"{""}"')"""

parse(to_source(parse(s1)))   # okay
parse(to_source(parse(s2)))  # raises an error

Also, is there a way that I can get around this before this bug is fixed? I have many s2 type source code snippets that should be parsed to an ast, be tweaked a little, and be reconstructed back to a snippet.

mathben commented 2 years ago

I have an similar error.

from ast import parse
from astor import to_source

s4 = '''str_1 = f"""["{'", "'.join(lst_item)}"]"""'''
parse(to_source(parse(s4)))

SyntaxError: f-string expression part cannot include a backslash

from ast import parse
from astor import to_source

s4 = '''str_1 = f"""["{'", "'.join(lst_item)}"]"""'''
to_source(parse(s4))

'str_1 = f\'["{\\'", "\\'.join(lst_item)}"]\'\n'

But expected

'str_1= f"""["{'", "'.join(lst_item)}"]"""\n'