A Short Introduction

Here is a short introduction to astng.

ASTNG - Abstract Syntax Tree New Generation - is an enhanced Python Syntax Tree Generator. It uses the tree generated from the '_ast' python module and rebuilds an new tree with more informations.

It is mainly used by Pylint, but also by Pyreverse and some other projects...

Main features

  • Represent Python Code in a nice Abstract Syntax Tree
  • try to get some information about not pure Python modules
  • handle complicated projects with a manager
  • Static Inference : try to guess the type and/or value of a variable
  • keep local informations about nodes (local name space, scope, line number,...)
  • representations of the tree : as tree, as string

An Example

Here is a file foo.py:

class Super(object):
    @classmethod
    def instance(cls):
        return cls()

class Sub(Super):
    def method(self):
        print 'method called'

sub = Sub.instance()

Some Use Cases

Astng builds a tree with the ASTNGBuilder from a file or from string.

The returned tree has a list of nodes in tree.body. Nodes in the dictionary tree.locals store locally defined variables.

Finally, inference is an important feature of astng. So let us see how all this works:

>>> from logilab.astng import builder as buildmod
>>> builder = buildmod.ASTNGBuilder()
>>> tree = builder.string_build(open("foo.py").read())
>>> # you can also write :
>>> tree = builder.file_build("foo.py")

>>> print ', '.join(str(n) for n in tree.body)
ClassDef(Super), ClassDef(Sub), Assign()
>>> for key, vals in tree.locals.items():
...     print key, ":", [str(n) for n in vals]
Super : [ClassDef(Super)]
Sub : [ClassDef(Sub)]
sub : [AssName(sub)]

>>> print list(tree['sub'].infer())
[<Instance of .Sub at 0x38697296>]

A sub tree

To understand the tree structure of the analyzed code, we can use the tree_repr method. Here is a tree representation for the sub assignment:

tree.body[2].tree_repr()

Assign()
  targets = [
  AssName(sub)
  ]
  value =
  Call()
      func =
      Attribute()
          expr =
          Name(Sub)
      args = [
      ]
      starargs =
      kwargs =

Applications

Pylint

pylint uses the ASTNG representation for analyzing code.

Pyreverse

Pyreverse builds nice UML diagrams from ASTNG projects. It is part of the pylint project.