google / pasta

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

rename_external breaking with ValueError #27

Closed williamjamir closed 6 years ago

williamjamir commented 6 years ago

Hi,

Running the rename_external on the following file test.py causes a ValueError: <_ast.Name object at 0x0000000005322F98> is not in list

File: test.py

from a.b import c

def ABC(oi=c):
    pass

Log:

(test_pasta) λ python s2.py
Traceback (most recent call last):
  File "s2.py", line 13, in <module>
    rename.rename_external(tree, 'a.b.c', 'a.b.d')
  File "w:\william\repo_pasta\pasta\augment\rename.py", line 92, in rename_external
    _rename_reads(sc, t, rename_old, rename_new)
  File "w:\william\repo_pasta\pasta\augment\rename.py", line 153, in _rename_reads
    ast.parse(new_name).body[0].value)
  File "w:\william\repo_pasta\pasta\base\ast_utils.py", line 240, in replace_child
    field_val[field_val.index(node)] = replace_with
ValueError: <_ast.Name object at 0x0000000005252F98> is not in list

Script:

with open('test.py', mode='r') as file:
    tree = pasta.parse(file.read())
    rename.rename_external(tree, 'a.b.c', 'a.b.d')

cc @nicoddemus

williamjamir commented 6 years ago

This same error happens in this situation too:

test.py

from a.b import c

def oi(self):
    foo = c

Script.py:

with open('test.py', mode='r') as file:
    tree = pasta.parse(file.read())
    rename.rename_external( tree , 'a.b.c' ,  'foo.b.d' )

Log:

Traceback (most recent call last):
  File "s2.py", line 18, in <module>
    rename.rename_external(tree, 'a.b.c', 'foo.b.c')
  File "w:\william\repo_pasta\pasta\augment\rename.py", line 92, in rename_external
    _rename_reads(sc, t, rename_old, rename_new)
  File "w:\william\repo_pasta\pasta\augment\rename.py", line 153, in _rename_reads
    ast.parse(new_name).body[0].value)
  File "w:\william\repo_pasta\pasta\base\ast_utils.py", line 240, in replace_child
    field_val[field_val.index(node)] = replace_with
ValueError: <_ast.Name object at 0x00000000051A8048> is not in list
soupytwist commented 6 years ago

Thanks for reporting it!