[code-quality] Rebuilding AST with Astroid?

Claudiu Popa pcmanticore at gmail.com
Fri Apr 24 00:21:14 CEST 2015


Hi Jarrad,


On Thu, Apr 23, 2015 at 7:43 PM, Jarrad Hope <me at jarradhope.com> wrote:
> 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.

It can definitely be improved. If you find something that can be
better explained,
don't hesitate to open a new issue.

>
> 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)

Most likely due to the weird extension.

>
> 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)

Indeed. For obtaining the string representation, just use node.as_string().
But do take in account the fact that what astroid returns when doing
.as_string()
might not be the actual code you passed it. That's a result of using ast.parse
for parsing the source code, ast.dump(ast.parse(X)) isn't always X.

>
> 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?

Actually we seems to lack a generic NodeVisitor implementation.
We have something in astroid.utils.ASTWalker, but it needs some
polishing before considering it. Here's what you can do until then:


from astroid.builder import AstroidBuilder
from astroid.utils import ASTWalker

builder = AstroidBuilder()
module = builder.file_build("a.py")

class Walker(object):

    def visit_getattr(self, node):
        print("getattr", node)

    def visit_module(self, node):
        print("module", node)

walker = ASTWalker(Walker())
walker.walk(module)


More information about the code-quality mailing list