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.8-3.11 interpreter to run pytype, as well as an
interpreter in $PATH
for the Python version of the code you're analyzing
(supported: 3.8-3.11).
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.
2023 Roadmap
- Typegraph rewrite for improved correctness and performance.
- Basic Python 3.11 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
File details
Details for the file pytype-2023.12.18.tar.gz
.
File metadata
- Download URL: pytype-2023.12.18.tar.gz
- Upload date:
- Size: 2.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3ffb2dcee71ddf95b4321b8eff066826a02e99336baa4d47851091a6bcc3457 |
|
MD5 | d4b16257080bb7b2c0417302db252a69 |
|
BLAKE2b-256 | 8f0eca94f77cb8d3618e601a738afc98b722a7de183f5dc9d106b161b9cd3e5f |
File details
Details for the file pytype-2023.12.18-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f42107fbac9b15458b3c7b94d3b86b3fe99566a5dad72ee86e9be8b2199c457 |
|
MD5 | 2bb8ddbe417e75480bbd26c44b9726c5 |
|
BLAKE2b-256 | cdb9bd50461ddcb0f19ddd9ebf7085e7ecc073aff8231c4d06666199b32198b7 |
File details
Details for the file pytype-2023.12.18-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 835b13b232005fe23f75f5f4958243f5833f028acc78fc7e7c3e45288e2d59a3 |
|
MD5 | e8497104e2533add34301300465c87c0 |
|
BLAKE2b-256 | 6ef36fb7ee11397c65cfe3b42c410a11c0bb7ca01691863c6caaaeb115a816ef |
File details
Details for the file pytype-2023.12.18-cp311-cp311-macosx_10_9_universal2.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp311-cp311-macosx_10_9_universal2.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.11, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ccc14ae3e76794b750d04a25cbe09803a54fe1638b514d539130802d49192d6 |
|
MD5 | 88e8e296802d72c91992d3c02d01498e |
|
BLAKE2b-256 | ef1f99e1607841578c5d711d390e32b8fb7a434d00f9438df0403c1171d120ab |
File details
Details for the file pytype-2023.12.18-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6014cc7e750da1c1c5af5f3428da1f2ee0c6921903a9ea6eaee0a975a4c5f11d |
|
MD5 | c50b4d37bda55daff6470a75bba6e262 |
|
BLAKE2b-256 | 7691851c842e9b202e31b4fd17309150076277eead28421362e22f653fe093ff |
File details
Details for the file pytype-2023.12.18-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5df2aaf8331f87d280480da2626eecba8992a04fefd82b060dbde2a55166862e |
|
MD5 | 56b424e4818a0556cb672ea404c400c4 |
|
BLAKE2b-256 | 049c6cac76a4a7521724f99b12863b4b39d2c9df697139eb9c576364cfa2fb38 |
File details
Details for the file pytype-2023.12.18-cp310-cp310-macosx_11_0_x86_64.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp310-cp310-macosx_11_0_x86_64.whl
- Upload date:
- Size: 4.2 MB
- Tags: CPython 3.10, macOS 11.0+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd1a76226ab2e1891eee561d606c8542e49287dbf72fe8dcf010459e2867e4ac |
|
MD5 | 88b984779ed530a0c33485c482172764 |
|
BLAKE2b-256 | 12ab1492d82d71529b1fc0b8564c677d7b9501074ca92ba817584179d97169ee |
File details
Details for the file pytype-2023.12.18-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c83832f915fcdf991f6463881022fb704af54b9138af469ed47b55292b34b6b |
|
MD5 | 8356ac142434373b37a6cef3aec8ab39 |
|
BLAKE2b-256 | 6de391079f4d7b4124eef61ecf5809db60deaebe076129d865c8c983dd75b168 |
File details
Details for the file pytype-2023.12.18-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14a9bacb67eb841c4db3f9f5b200f257b6b556bc91172eada7caa1e07b4b762e |
|
MD5 | 7a8539db4003876a7efcae4bc3792c39 |
|
BLAKE2b-256 | 510492369a7eaf69fa7cb3fd250a25dc2c26f621caad2846116848c7edab04a4 |
File details
Details for the file pytype-2023.12.18-cp39-cp39-macosx_11_0_x86_64.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp39-cp39-macosx_11_0_x86_64.whl
- Upload date:
- Size: 4.2 MB
- Tags: CPython 3.9, macOS 11.0+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5409ff4816e6cfca22c248d1bea4c152dce0c4dc4ec65a527238d6ce0b270e7 |
|
MD5 | d094d171974dd238e027d7173c4512ff |
|
BLAKE2b-256 | e0d077e175918027c1af80712d203a5176075fd10e64a073d57472d78d460500 |
File details
Details for the file pytype-2023.12.18-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf0d02e5385725fff2b177ed945792407139a13c3f34e7218a0cb6c3026c8563 |
|
MD5 | 5674ddc4d3fc384e7b08b0396b44f259 |
|
BLAKE2b-256 | 84375b9fbe5fc0e7cccc3cf92a77562ccf98cdef0e950f53930a4d9cbc7ec0ac |
File details
Details for the file pytype-2023.12.18-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.8, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f48410a73e14fd863da3b3d3ff5d01716cd6b3ae1ceb23db577d6aed9629aab6 |
|
MD5 | ecd5230667471a1507626ef548de2d33 |
|
BLAKE2b-256 | 3558e0b955c496bcf8aa4a4af833abbaeecbebfbb4e206edc3a78bcccfc37a42 |
File details
Details for the file pytype-2023.12.18-cp38-cp38-macosx_11_0_x86_64.whl
.
File metadata
- Download URL: pytype-2023.12.18-cp38-cp38-macosx_11_0_x86_64.whl
- Upload date:
- Size: 4.2 MB
- Tags: CPython 3.8, macOS 11.0+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cd1a8f6039abe5a15af8c98d3c6cd896c820ea40d472b21efd701ddd855f7f3 |
|
MD5 | 91a9e993980fe9a5bdd36afe63f345ac |
|
BLAKE2b-256 | c6707d4b6cff5afd9446ffbef49ad1040c1f4408ffa81742f1eed1ba23ec3b26 |