Quickstart
To quickly get started with type-checking a file or directory, run the
following, replacing file_or_directory
with your input:
pip install pytype
pytype file_or_directory
To set up pytype on an entire package, add the following to a pyproject.toml
file in the directory immediately above the package, replacing package_name
with the package name:
[tool.pytype]
inputs = ['package_name']
Now you can run the no-argument command pytype
to type-check the package. It’s
also easy to add pytype to your automated testing; see this
[example][importlab-github-actions] of a GitHub project that runs pytype on GitHub Actions.
Finally, pytype generates files of inferred type information, located by default
in .pytype/pyi
. You can use this information to type-annotate the
corresponding source file:
merge-pyi -i <filepath>.py .pytype/pyi/<filename>.pyi
Installing on WSL
Follow the steps above, but make sure you have the correct libraries first:
sudo apt install build-essential python3-dev libpython3-dev
Config File
For convenience, you can save your pytype configuration in a file. The config
file can be a TOML-style file with a [tool.pytype]
section (preferred) or an
INI-style file with a [pytype]
section. If an explicit config file is not
supplied, pytype will look for a pytype section in the first pyproject.toml
or
setup.cfg
file found by walking upwards from the current working directory.
Start off by generating a sample config file:
$ pytype --generate-config pytype.toml
Now customize the file based on your local setup, keeping only the sections you need. Directories may be relative to the location of the config file, which is useful if you want to check in the config file as part of your project.
For example, suppose you have the following directory structure and want to
analyze package ~/repo1/foo
, which depends on package ~/repo2/bar
:
~/
├── repo1
│ └── foo
│ ├── __init__.py
│ └── file_to_check.py
└── repo2
└── bar
├── __init__.py
└── dependency.py
Here is the filled-in config file, which instructs pytype to type-check
~/repo1/foo
as Python 3.9 code, look for packages in ~/repo1
and ~/repo2
,
and ignore attribute errors. Notice that the path to a package does not include
the package itself.
$ cat ~/repo1/pytype.toml
# NOTE: All relative paths are relative to the location of this file.
[tool.pytype]
# Space-separated list of files or directories to process.
inputs = [
'foo',
]
# Python version (major.minor) of the target code.
python_version = '3.9'
# Paths to source code directories, separated by ':'.
pythonpath = .:~/repo2
# Space-separated list of error names to ignore.
disable = [
'attribute-error',
]
We could’ve discovered that ~/repo2
needed to be added to the pythonpath by
running pytype’s broken dependency checker:
$ pytype --config=~/repo1/pytype.toml ~/repo1/foo/*.py --unresolved
Unresolved dependencies:
bar.dependency
Subtools
Pytype ships with a few scripts in addition to pytype
itself:
annotate-ast
, an in-progress type annotator for ASTs.- [
merge-pyi
][merge-pyi], for merging type information from a .pyi file into a Python file. pytd-tool
, a parser for .pyi files.pytype-single
, a debugging tool for pytype developers, which analyzes a single Python file assuming that .pyi files have already been generated for all of its dependencies.pyxref
, a cross-references generator.