Skip to content
Michael J. Sullivan edited this page Dec 5, 2017 · 8 revisions

The mypy parser uses the CPython "ast" module to convert source code into an abstract syntax tree (AST). (The term parse tree is sometimes used informally as a synonym.)

You can use the parse.py module as a script to experiment with it (I assume that mypy is an alias to the mypy.py script):

$ cd mypy         # mypy repo
$ cat samples/hello.py
print('Hello, world')
$ mypy parse.py samples/hello.py
MypyFile:1(
  samples/hello.py
  ExpressionStmt:1(
    CallExpr:1(
      NameExpr(print)
      Args(
        StrExpr(Hello, world)))))

The names MypyFile, ExpressionStmt, CallExpr, NameExpr and StrExpr refer to AST node classes defined in mypy/nodes.py. The numbers after colons are line numbers.

The parser does only a minimal amount of consistency checks. As such it also accepts many invalid programs. The next compiler pass, SemanticAnalyzer, performs additional checks.