I am trying to traverse through ast nodes in Python to fetch value node's Name ids for all Assign Nodes. Ast.walk() method gives me nodes randomly.
Assumption : Only for Assign nodes
for node in ast.walk(tree):
if isinstance(node,(ast.Assign)):
if isinstance(node.value,(ast.Name)):
print('Assign Value:-','lineno: ',node.value.lineno,' id :',node.value.id)
if isinstance(node.value,(ast.Tuple)):
for i in range(len(node.value.elts)):
if isinstance(node.value.elts[i],(ast.Name)):
print('Assign Value:-','lineno: ',node.value.elts[i].lineno,' id :',node.value.elts[i].id)
Here, sometimes ast gives BinOps, Call etc inside value node. I have to get Name.id inside value node.
Ex: a,b = (c+d), e --> Expected Values should be c,d,e..but I get only e . Also, how to tag (c,d) to target 'a' .
Target can be fetched from Assign Node .