google / pasta

Library to refactor python code through AST manipulation.
Apache License 2.0
341 stars 40 forks source link

Whitespace different for methods with tuples at end #13

Closed akov closed 6 years ago

akov commented 6 years ago
In [25]: code = """\                                                                                                                                                                                                
class Foo():
  def a(self):
    return 1

  def b(self):
    return 2

  def c(self):
    return 3
"""

In [26]: tree = pasta.parse(code)                                                                                                                                                                                   

In [27]: tree.body[0].body.pop(1).name                                                                                                                                                                              
Out[27]: 'b'

In [28]: print pasta.dump(tree)                                                                                                                                                                                     
class Foo():
  def a(self):
    return 1

  def c(self):
    return 3

In [29]: code = """\                                                                                                                                                                                                
class Foo():
  def a(self):
    return 1

  def b(self):
    return 2,'a'

  def c(self):
    return 3
"""

In [30]: tree = pasta.parse(code)

In [31]: tree.body[0].body.pop(1).name
Out[31]: 'b'

In [32]: print pasta.dump(tree)
class Foo():
  def a(self):
    return 1
def c(self):
    return 3

The first snippet here acts as expected. I would expect the result of the second snippet to be the same, but in reality a newline seems to get lost.

soupytwist commented 6 years ago

It's fixed in master. This is a more general issue of parsing a suffix for child nodes. Now, the formatting data for space between elements of a tuple is stored on the tuple rather than on the children.