I’m using _ast to do some code analysis and have run into an issue with getting the name of an imported function.
Suppose my code file (code.py) looks like this:
import somemod
def foo():
somemod.bar()
When I get the ast for this file with root = compile(open('codefile.py').read(), 'codefile.py', 'exec', _ast.PyCF_ONLY_AST), I can get the line calling somemod.bar like this:
root.body[1].body[0]
This is an _ast.Expr node.
I then expect to be able to get somemod.bar by looking into this node. However, when I look at root.body[1].body[0].value.func.value.id, I get somemod. As far as I can tell, I can’t find a way to get bar or somemod.bar
What am I missing? How can I get at the bar?
It’s in
value.func.attr: