Pytype
Pytype is a static type analyzer for Python that infers and checks types in Python code, even without explicit type annotations. It can lint plain Python code for common mistakes, enforce user-provided type annotations, and generate `.pyi` stub files. Developed by Google, Pytype is currently in maintenance mode, with Python 3.12 being the last officially supported version, focusing on bugfixes rather than new features.
Warnings
- breaking Pytype is in maintenance mode; Google has discontinued active development, and Python 3.12 is the last supported version. No new features will be added, only bugfixes. Users are encouraged to explore alternative type checkers like Mypy, Pyright, ty, or Pyrefly.
- gotcha Pytype's default behavior is lenient compared to other type checkers like Mypy. It allows operations that succeed at runtime and don't contradict annotations, which might lead to different results, particularly with container types or `Optional` handling.
- gotcha For silencing specific errors, use `pytype: disable=error-class` on the line or block, or `type: ignore`. Disabling errors for an entire file can be done by placing a disable directive on its own line at the start of the file.
- gotcha Performance can degrade significantly on very large Python files (anecdotally, over ~1500 lines). Extensive inference on complex, unannotated code can be slow.
- gotcha Some experimental features, such as `...` as a top-level annotation for 'inferred type', are unique to Pytype and not supported by other type checkers (e.g., Mypy, Pyright). Using these can lead to errors when analyzing code with other tools.
- gotcha Pytype is primarily developed and tested on Linux. While it supports macOS (10.7+ and Xcode 8+), Windows support typically requires the Windows Subsystem for Linux (WSL).
Install
-
pip install pytype
Quickstart
# To check a single file or directory:
# pytype my_module.py
# pytype my_package/
# For package-wide configuration using pyproject.toml:
# 1. Create a pyproject.toml in your project root with the following:
# [tool.pytype]
# inputs = ['your_package_name']
# 2. Then run pytype without arguments:
# pytype
# Example Python file (example.py):
def greet(name):
return "Hello, " + name
def add(a, b):
return a + b
# Run pytype on this file from your terminal:
# pytype example.py
# Expected output for add function (e.g., if you call add(1, '2')):
# File "example.py", line 5, in add: unsupported operand type(s) for +: 'int' and 'str' [unsupported-operands]