astmonkey icon indicating copy to clipboard operation
astmonkey copied to clipboard

source code generation bugs

Open aniskoff opened this issue 4 years ago • 0 comments

Hi! I was using astmonkey for the purposes of my project and notice several bugs in code generation (visitors.to_source) function. The code snippets below (the original code and the code generated from the original's AST) illustrate the problems:

  1. visitors.to_source doesn't distinguish single-quoted strings (like 'hello') from double-quoted strings (like "hello"). This is a problem because statements like 'this is "double quotes" inside single quotes' won't be proceeded correctly. E.g. code generated from AST of the code below
s = 'this is "double quotes" inside single quotes'

will become

'this is 'double quotes' inside single quotes'

and that will be a SyntaxError.

  1. visitors.to_source doesn't account async's in comprehension's (like [i async for i in some_iterable])
[i async for i in some_iterable]

will become

[i for i in some_iterable]

i.e. async is lost.

  1. visitors.to_source doesn’t support AnnAssign (annotated assignment like a: int = 1 ) which was introduced in Python 3.6
a: int = 1

will become

aint2

and that will be a SyntaxError.

  1. visitors.to_source works incorrectly on more than one decorator (both for functions and classes). Example with function:
@dec1
@dec2
def foo():
	pass

will become

@dec1@dec2
def f():pass

and that will be a SyntaxError.

  1. visitors.to_source still works incorrectly on else in try except stmt. Example:
try:
    do_smth1()
except SomeError as e:
    do_smth2()
else:
    do_smth3()
finally:
    do_smth4() 

will become

try:
    do_smth1()
except SomeError as e:
    do_smth2()
finally:
    
    
    do_smth4()
    l += 2else:do_smth3()

and that will be a SyntaxError.

Best regards, Nick

aniskoff avatar Aug 04 '20 10:08 aniskoff