Closed cabiad closed 5 years ago
I think I got it working:
$ cat use_cases.py
from get_user import C
c = C()
c.get_user()
$ cat get_user.py
class C(object):
def get_user(self):
return "abc"
>>> import astroid
>>> m = astroid.parse(open('use_cases.py').read())
>>> next(m.body[2].value.infer()).frame().parent.name
'C'
Does this work for you?
print(next(ext_obj.value.infer()))
hmmm I made the example a little more complex and it's no longer working :(
edit: this seems to work with this example:
# tree is the top-level tree returned by `ast.parse`
tree.infer()
print(m.body[2].value.args[0].func.inferred()[0].frame().parent)
Nice! I was thinking this morning that the python instance where you run astroid.parse()
probably needs to be able to import the same dependencies as the file you're parsing the AST of, so easiest is to run it in an interpreter with the same context as your code (which I wasn't doing). I had assumed that with those extra args that it'd be able to complete relative imports. Trying it in a trivial project was my next step for this morning. :)
Now the next step is to try it in a larger project...
Works great in this larger project, except that there are some Django-specific inference limitations that we can work around later.
First attempt at using
astroid
. It seems really cool, but I haven't yet figured out how to connect to the context about types that exist outside of the AST / file it is currently parsing.Here's an edited and annotated session with it in a
python
shell working on some tests for a specific project.