[code-quality] Rebuilding AST with Astroid?

Jarrad Hope me at jarradhope.com
Thu Apr 23 18:43:00 CEST 2015


Hello

I am currently using the Python ast package with ast.NodeVisitor to
generate code for another VM. I found Astroid during my search for a
type inference solution.

The documentation is abit sparse for me, and I was hoping someone
could help me with extending the AST with Astroid's.

At first I tried AstroidManager().ast_from_file(f)

but I got astroid.exceptions.AstroidBuildingException: Unable to load
module tests/single_function.se (No module named
tests/single_function.se)

I then tried something like
ast_se = ast.parse(code)
module_se = TreeRebuilder(AstroidManager()).visit_module(ast_se,
"blah", "blah", "blah")

Which gets me a module, but I couldn't seem to pass it to
MyNodeVisitor.visit(module_se)

or ast.dump(module_se) (not surprising as it's not an AST)

So my question is, how do I build an AST with Astroid from a
stand-alone file, walk the ast in a fashion similar to NodeVisitor,
and if possible query types from the extended AST nodes?

Thanks,
Jarrad


More information about the code-quality mailing list