Skip to content

Implementation Overview

Elazar Gershuni edited this page Jul 25, 2018 · 16 revisions

This is a general introduction to the mypy implementation. The linked subpages have more detailed information. The file links point to the mypy git repository.

Structure of the type checker

The main entry point of mypy is mypy/main.py. This file is responsible for parsing the config file, parsing command line options, and handling other related boilerplate.

The flow of execution is then passed off to the Build Manager (mypy/build.py), which is responsible for coordinating a build, managing dependencies between modules, and performing compilation passes in the correct order.

The build manager will then perform several passes over each module to typecheck them (see the process_graph function for more details).

Common passes:

  1. Python Parser (mypy/parse.py, mypy/fastparse.py, mypy/fastparse2.py)
    • Uses the typed_ast to build an abstract syntax tree (AST). The typed_ast library is nearly identical to CPython's "ast" module except that it has better support for parsing things like type comments.
    • This AST is then converted into mypy-specific parse tree, which is defined in the files mypy/nodes.py and mypy/types.py
  2. Semantic Analyzer (mypy/semanal.py, mypy/semanal_*.py, mypy/typeanal.py)
    • Binds names to definitions
    • Performs various consistency checks
  3. Type Checker (mypy/checker.py, mypy/checkexpr.py, mypy/checkmember.py)
    • Type checks the program
    • Performs type inference