Pytype

Pytype is a static analysis tool that derives types from your Python code without the need for type annotations. However, it can also enforce type annotations that are in the code. Although annotations are optional for Pytype, they are checked and applied if they are present. The type annotations generated by Pytype are stored in standalone .pyi files, which can be merged back into Python using merge-pyi. Finally, it flags common errors such as misspelled attribute names or function calls and much more, even across file boundaries.

Requirements

  • All common Linux distributions are supported

  • macOS ≥ 10.7 and Xcode ≥ 8

  • Windows with WSL. In addition, the following libraries must be installed:

    $ sudo apt install build-essential python3-dev libpython3-dev
    

Installation

Pytype can be easily installed with

$ uv add pytype

The installation can then be checked with

$ uv run pytype file_or_directory

Configuration

For a Python package, you can set up Pytype by creating a pytype.cfg file with

$ uv run pytype --generate-config pytype.cfg

This then starts with for example

# NOTE: All relative paths are relative to the location of this file.

[pytype]

# Space-separated list of files or directories to exclude.
exclude =
    **/*_test.py
    **/test_*.py

# Space-separated list of files or directories to process.
inputs =
    .

Now you can customise the configuration file according to your requirements.

Additional scripts

annotate-ast

in-progress type annotator for ASTs

merge-pyi

Merge type information from a .pyi file into a Python file

pytd-tool

parser for .pyi files

pytype-single

debugging tool for pytype developers that analyses a single python file assuming that .pyi files have already been generated for all dependencies

pyxref

cross-references generator