PyCQA / redbaron

Bottom-up approach to refactoring in python
http://redbaron.pycqa.org/
694 stars 74 forks source link

List without training comma as a kwarg raises IndentationError #124

Open illicitonion opened 7 years ago

illicitonion commented 7 years ago

Python being analysed:

def foo(**kawrgs):
    pass

foo(
  arg = [ 
    'foo'
  ],  
)

Code using redbaron:

import redbaron

with open('test.py', 'r') as f:
    ast = redbaron.RedBaron(f.read())

xs = ast.find('CallArgumentNode').value.value
for x in xs: 
    print x.to_python()

Expect "foo" to be printed. Actually get:

Traceback (most recent call last):
  File "rb.py", line 8, in <module>
    print x.to_python()
  File "/Users/dwh/.virtualenv/redbaron/lib/python2.7/site-packages/redbaron/base_nodes.py", line 136, in to_python
    return ast.literal_eval(self.dumps())
  File "/opt/homebrew/Cellar/python27/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 49, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/opt/homebrew/Cellar/python27/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 37, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 2

    ^
IndentationError: unexpected indent

Adding a trailing comma after "foo" makes this work, but isn't required by python. Analysing the same list but as a top-level assignment rather than a kwarg value works fine.

duncf commented 7 years ago

This was fixed with commit 9082c8c295e3b7f40880b241cd27242af3c8c760.