Statically check and infer types for unannotated Python code. (This is not an official Google product.)
Pytype is a static analyzer that helps you find type errors in Python code. It can type-check code with or without type annotations, as well as generate annotations. Pytype runs under Python 2.7 or 3.6 and analyzes both Python 2 and Python 3 code.
Below, print_greeting
calls make_greeting
incorrectly:
$ cat foo.py
def make_greeting(user_id):
return 'hello, user' + user_id
def print_greeting():
print(make_greeting(0))
Run pytype to catch the bug:
$ pytype foo.py
File "foo.py", line 2, in make_greeting: Function str.__add__ was called with the wrong arguments [wrong-arg-types]
Expected: (self, y: str)
Actually passed: (self, y: int)
Traceback:
line 5, in print_greeting
Merge pytype's generated type information back into foo.py
:
$ cat pytype_output/foo.pyi
def make_greeting(user_id) -> str: ...
def print_greeting() -> None: ...
$ merge-pyi -i foo.py foo.pyi
$ cat foo.py
def make_greeting(user_id) -> str:
return 'hello, user' + user_id
def print_greeting() -> None:
print(make_greeting(0))
Pytype is currently available only on Linux. You need a Python 2.7 or 3.6
interpreter to run pytype, as well as an interpreter in $PATH
for the Python
version of the code you're analyzing.
Installation on windows (either from pip or from github) requires the Visual C++ compiler, which can be downloaded from Microsoft. This page lists the version of the compiler you need, depending on your Python version.
Pytype can be installed via pip:
pip install wheel
pip install pytype
Or from the source code on GitHub.
(Note that pytype's setup.py
relies on setuptools).
git clone --recurse-submodules https://github.com/google/pytype.git
cd pytype
pip install -U .
Instead of using --recurse-submodules
, you could also have run
git submodule init
git submodule update
in the pytype
directory.
usage: pytype [options] input [input ...]
positional arguments:
input file or directory to process
Common options:
-V, --python-version
: Python version (major.minor) of the target code. Defaults to3.6
.-o, --output
: The directory into which all pytype output goes, including generated .pyi files. Defaults topytype_output
.-P, --pythonpath
. Paths to source code directories, separated by ':'. Defaults to an educated guess based oninput
.-d, --disable
. Comma separated list of error names to ignore. Detailed explanations of pytype's error names are in this doc. Defaults to empty.
For a full list of options, run pytype --help
.
In addition to the above, you can direct pytype to use a custom typeshed
installation instead of its own bundled copy by setting $TYPESHED_HOME
.
For convenience, you can save your pytype configuration in a file. The config
file is 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
setup.cfg
file found by walking upwards from the current working directory.
Start off by generating a sample config file:
$ pytype --generate-config pytype.cfg
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 treat its input as Python 3.6 code and ignore attribute errors. Notice that the path to a package does not include the package itself.
$ cat ~/repo1/pytype.cfg
# NOTE: All relative paths are relative to the location of this file.
[pytype]
# Python version (major.minor) of the target code.
python_version = 3.6
# Paths to source code directories, separated by ':'.
pythonpath =
.:
~/repo2
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.cfg ~/repo1/foo/*.py --unresolved
Unresolved dependencies:
bar.dependency
Pytype ships with three scripts in addition to pytype
itself:
merge-pyi
, for merging type information from a .pyi file into a Python file.pytd
, 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.
- A rerun mode to only reanalyze files that have changed since the last run
Apache 2.0