berkerpeksag / astor

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

astor.dump_tree doesn't populate ctx. #188

Open stuaxo opened 4 years ago

stuaxo commented 4 years ago

I've started playing with astor and ASTs, and noticed astor doesn't populate ctx when using dump_tree, this makes means there is an extra step to fill this out before the output can be used for NodeTransformer etc.

Astor dump - no ctx:

print(astor.dump_tree(parse('a = variables[1]')))
Module(
    body=[
        Assign(targets=[Name(id='a')],
            value=Subscript(value=Name(id='variables'), slice=Index(value=Constant(value=1, kind=None))),
            type_comment=None)],
    type_ignores=[])

dump_tree from greentreesnakes:

print(dump(parse('a = variables[1]')))
Module(body=[
    Assign(targets=[
        Name(id='a', ctx=Store()),
      ], value=Subscript(value=Name(id='variables', ctx=Load()), slice=Index(value=Constant(value=1, kind=None)), ctx=Load()), type_comment=None),
  ], type_ignores=[])