A static type analyzer for Python code

Developer guide
Development process
Python version upgrades
Supporting new features
Program analysis
Main loop
Stack frames
Data representation
Abstract values
Special builtins
Type annotations
Type stubs
Style guide
Documentation debugging

View the Project on GitHub google/pytype

Hosted on GitHub Pages — Theme by orderedlist

Directives and annotations


Pytype accepts directives in the form of python comments, both typecomments like

  x = []  # type: List[int]

and error disabling

  x = f(a, b)  # pytype: disable=wrong-arg-types

We also support range-based disabling:

  # pytype: disable=attribute-error
  # pytype: enable=attribute-error

Since the main pytype loop operates on bytecode, which does not contain comments, we have a preprocessing pass to collect these comments and later merge them with the corresponding line numbers in the bytecode.

Director defines a Director class, which scans a source file line by line, extracting and storing source level information for the main code to use when analysing the program.

Note: The Director is also the best place to collect other information that needs access to the source code rather than the bytecode, e.g. variable annotations, class and method decorators, and docstrings. See Director.__init__ for a full list of all the data collected.

The Director is instantiated at the start of the main loop, in The rest of the code uses the director in two ways:

  1. Directly accessing the stored tables, e.g. director.decorators and director.annotations, all of which are indexed by line number
  2. Calling director.filter_error() to check if an error has been disabled

The latter method is needed because error disabling is range based, so checking is not as simple as if line_number in director.disables.