Pytype

2024.10.11 · maintenance · verified Sun Apr 12

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

Install

Quickstart

Pytype is primarily a command-line tool. To quickly get started, run `pytype` followed by the file or directory you wish to analyze. For project-level configuration, use a `pyproject.toml` file to specify inputs and settings.

# 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]

view raw JSON →