Python type inferencer
Project description
pytype - 🦆✔
Pytype checks and infers types for your Python code - without requiring type annotations. Pytype can:
- Lint plain Python code, flagging common mistakes such as misspelled attribute names, incorrect function calls, and much more, even across file boundaries.
- Enforce user-provided type annotations. While annotations are optional for pytype, it will check and apply them where present.
- Generate type annotations in standalone files ("pyi files"), which can be merged back into the Python source with a provided merge-pyi tool.
Pytype is a static analyzer; it does not execute the code it runs on.
Thousands of projects at Google rely on pytype to keep their Python code well-typed and error-free.
For more information, check out the user guide, FAQ, or supported features.
How is pytype different from other type checkers?
-
Pytype uses inference instead of gradual typing. This means it will infer types on code even when the code has no type hints on it. So it can detect issues with code like this, which other type checkers would miss:
def f(): return "PyCon" def g(): return f() + 2019 # pytype: line 4, in g: unsupported operand type(s) for +: 'str' # and 'int' [unsupported-operands]
-
Pytype is lenient instead of strict. That means it allows all operations that succeed at runtime and don't contradict annotations. For instance, this code will pass as safe in pytype, but fail in other type checkers, which assign types to variables as soon as they are initialized:
from typing import List def get_list() -> List[str]: lst = ["PyCon"] lst.append(2019) return [str(x) for x in lst] # mypy: line 4: error: Argument 1 to "append" of "list" has # incompatible type "int"; expected "str"
Also see the corresponding FAQ entry.
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 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
Requirements
You need a Python 3.7-3.10 interpreter to run pytype, as well as an
interpreter in $PATH
for the Python version of the code you're analyzing
(supported: 3.7-3.10).
Platform support:
- Pytype is currently developed and tested on Linux*, which is the main supported platform.
- Installation on MacOSX requires OSX 10.7 or higher and Xcode v8 or higher**.
- Windows is currently not supported unless you use WSL.
*
On Alpine Linux, installation may fail due to issues with upstream
dependencies. See the details of this issue for a
possible fix.
**
If the ninja dependency fails to install, make sure cmake is installed. See
this issue for details.
Installing
Pytype can be installed via pip. Note that the installation requires wheel
and setuptools
. (If you're working in a virtualenv, these two packages should
already be present.)
pip install pytype
Or from the source code on GitHub.
git clone --recurse-submodules https://github.com/google/pytype.git
cd pytype
pip install .
Instead of using --recurse-submodules
, you could also have run
git submodule init
git submodule update
in the pytype
directory. To edit the code and have your edits tracked live,
replace the pip install command with:
pip install -e .
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
Usage
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 to the version that pytype is running under.-o, --output
: The directory into which all pytype output goes, including generated .pyi files. Defaults to.pytype
.-d, --disable
. Comma or space-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
.
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
, 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.
2022 Roadmap
- Complete Python 3.9 feature support.
- Basic Python 3.10 support.
License
Disclaimer
This is not an official Google product.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pytype-2023.2.17-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26ef8cf21e5a96fa969ad8556a255004199b00aa6a3bb30a6f5cc7aa49eeb532 |
|
MD5 | 39019415502b8c8d413a1002f6c5fa8d |
|
BLAKE2b-256 | 89943712630297e9e3a51297377854a334620f31c09652746f51eedd81ee22d2 |
Hashes for pytype-2023.2.17-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf37b39fa85b8cd0548b0d2b00371737fbd56bd816b1b9b4014db47d21cd22aa |
|
MD5 | 94ae5027969ceeee6167353c47849d61 |
|
BLAKE2b-256 | 5503316c300c602bf78e046b0e707f1e7ed104fd3af997c6c6d741f010394d3d |
Hashes for pytype-2023.2.17-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 843c0bd929e63dc797e671cd94d1378986e862a41ba567dc4b81f6251e90fb93 |
|
MD5 | 54b63a35c8b7c9dcb19b39f12d504ef3 |
|
BLAKE2b-256 | 4feb3f7d8357795d5f54583e9f6d60694bab7d887b7d6cef0731fa5e827a5c39 |
Hashes for pytype-2023.2.17-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de630e36c3d347b94924d63f448183a188de1b3fac2f72f86846a9f101f30d6b |
|
MD5 | 03c87c0463fc09ef0d37eb37c4c78133 |
|
BLAKE2b-256 | 7e7caf0308fa23db281692d0696785739afe0252a492bf8ac1463ae365ae7f90 |
Hashes for pytype-2023.2.17-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d87718e276429f4809605ff3d7e0d16ad8a298f6b6c381b033a4e3b2d97a8d9d |
|
MD5 | 790b2e42626b449faca38e70ab3c7145 |
|
BLAKE2b-256 | 390593a6d7735757f77867dda53269e857b4a5f6216e54b8a6568158b82e2d9c |
Hashes for pytype-2023.2.17-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dcc358fc24f97df2ecf08e41c989ad31dc6caea9a5441abe00e39077a478965 |
|
MD5 | 640b529d8586c5e20ce49abba6054b7b |
|
BLAKE2b-256 | df55d0f829e7a2f2f943c4f1c29bd403901263e05921cc7f9f6a0e7e221d7a70 |
Hashes for pytype-2023.2.17-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a166401c3084bbede5cebb340210ebbc9014bfb446291afe1c3df987c941f0c1 |
|
MD5 | 1d6c4cc55c40ba69df4797b13fd6847d |
|
BLAKE2b-256 | 09d57041463210047e196174ea080201cd6368873cf1243eaee0b3c2a1178a99 |
Hashes for pytype-2023.2.17-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0cc622ee3748b5fdbe33ee5353d01033b84ad1070c55ef06afc89d361bcedfe |
|
MD5 | 6edb6d8410201a469011e1e34b4f3989 |
|
BLAKE2b-256 | ac636984ea1ceda6abfd52946ddc770e8d8b55d9e5286bde3e2c5fbe5244a104 |
Hashes for pytype-2023.2.17-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6acd33a589ecde32e5fde45a69fa5b8b394f1bb1b6c04ecb026e1db40ed0ea08 |
|
MD5 | 75c6e3d6c3d2cdb209e47382617a9741 |
|
BLAKE2b-256 | faa785c8b87f1b1a8f65f8981f0b3052f4ef25bd3943f0d1bc5eb099929ba707 |
Hashes for pytype-2023.2.17-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d59c9915c5819b2633624a80d17afd6413864b4edb457f064d220e5df3c9f85 |
|
MD5 | 72cedbea913bf5871f59667a6ef1fe04 |
|
BLAKE2b-256 | ea6af55c4c0cbf077d76dd541c608e3f77d50c706806574fed91b246ba869482 |
Hashes for pytype-2023.2.17-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef1b2444b61a855c783f7a9d41594783a129f7b4d8b2658bab5b772d08834e2b |
|
MD5 | d8127be1776e8f3ae7bdd8e8b8f258b8 |
|
BLAKE2b-256 | 2c715033dc6dbfa07698787ffa57836b821a09b0b141adfed9ff9f03a0fabdeb |
Hashes for pytype-2023.2.17-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66029c3dca4630d1f0ad0889627d768f40779d2e87204e3c614438b50ddc2918 |
|
MD5 | 5f922e719456140e2e915af35c1c01d6 |
|
BLAKE2b-256 | 83f1bf503d56c404b8c44af4c26720fcc24911a7264ca56bd5e8d22d26be30a0 |