google/pasta

Doesn't respect comments between functions

akov opened this issue · 0 comments

akov commented

When you try to remove a function it seems to count comments after it that are clearly not on the indentation level of the body of the function as part of the function and they seem to get removed.

In [4]: src = """\
   ...: x = 3
   ...: def f(y):
   ...:   return y + 2
   ...: # lol
   ...: def g(y):
   ...:   return y + 7
   ...: """

In [5]: n = pasta.parse(src)

In [6]: n
Out[6]: <_ast.Module at 0x3ac1750>

In [7]: n.body
Out[7]: 
[<_ast.Assign at 0x3ac1790>,
 <_ast.FunctionDef at 0x3ac1b10>,
 <_ast.FunctionDef at 0x3ac19d0>]

In [8]: n.body = n.body[n.body[0], n.body[2]]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-5d440e76f12a> in <module>()
----> 1 n.body = n.body[n.body[0], n.body[2]]

TypeError: list indices must be integers, not tuple

In [9]: n.body = [n.body[0], n.body[2]]

In [10]: n.body
Out[10]: [<_ast.Assign at 0x3ac1790>, <_ast.FunctionDef at 0x3ac19d0>]

In [11]: pasta.dump(n)
Out[11]: 'x = 3\ndef g(y):\n  return y + 7\n'

In [12]: print pasta.dump(n)
x = 3
def g(y):
  return y + 7