Skip to content

source code generation bugs #22

@aniskoff

Description

@aniskoff

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions