Skip to main content

Python type inferencer

Project description

CI PyPI - Wheel

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?

  1. 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]
    
  2. 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 setup.cfg file in the directory immediately above the package, replacing package_name with the package name:

[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.6-3.9 interpreter to run pytype, as well as an interpreter in $PATH for the Python version of the code you're analyzing (supported: 3.6-3.9).

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, installing 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 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 type-check ~/repo1/foo as Python 3.6 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.cfg

# NOTE: All relative paths are relative to the location of this file.

[pytype]

# Space-separated list of files or directories to process.
inputs =
    foo

# Python version (major.minor) of the target code.
python_version = 3.6

# Paths to source code directories, separated by ':'.
pythonpath =
    .:
    ~/repo2

# Comma or 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.cfg ~/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.

2021 Roadmap

  • Python 3.9 support
  • Better performance on large files
  • Support for numerical libraries

License

Apache 2.0

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

pytype-2021.12.8.tar.gz (2.8 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pytype-2021.12.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

pytype-2021.12.8-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ ARM64

pytype-2021.12.8-cp39-cp39-macosx_10_14_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.9macOS 10.14+ x86-64

pytype-2021.12.8-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

pytype-2021.12.8-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ ARM64

pytype-2021.12.8-cp38-cp38-macosx_10_14_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.8macOS 10.14+ x86-64

pytype-2021.12.8-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

pytype-2021.12.8-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ ARM64

pytype-2021.12.8-cp37-cp37m-macosx_10_14_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.7mmacOS 10.14+ x86-64

pytype-2021.12.8-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.17+ x86-64

pytype-2021.12.8-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.17+ ARM64

pytype-2021.12.8-cp36-cp36m-macosx_10_14_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.6mmacOS 10.14+ x86-64

File details

Details for the file pytype-2021.12.8.tar.gz.

File metadata

  • Download URL: pytype-2021.12.8.tar.gz
  • Upload date:
  • Size: 2.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for pytype-2021.12.8.tar.gz
Algorithm Hash digest
SHA256 ff9c3b854570a681348095f139d25050bf217c81a43db5880637de763dc9ee5b
MD5 be72bc35794351c22064ec788720057e
BLAKE2b-256 1edc33e577be3a242da2c0ae0b8ace4d5e5f6b1e8a7b62ff117b7e01112cf475

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2021.12.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d38936bde0594136271532965ef9766b0822c2ee77b5552306c68605204e0ea4
MD5 3b9f3a48692b788002d381ce6ff4e563
BLAKE2b-256 3aace8edc7f29943f9ae22787c7b6d45481bdb5f3a5471b8951d804fa159ccd3

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pytype-2021.12.8-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 cfb9fbd65e656382eb931a8871e96b9fefcf7169212e5feb7ba238a1f8903e47
MD5 4424eb46b9e0ac0e12ea60fd909717dc
BLAKE2b-256 bc68c0c0a2c821f5f5bf21c526bcf526e6b8716ec92d91b14d47b617de860840

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: pytype-2021.12.8-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.9, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for pytype-2021.12.8-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ef0af4068a90a79e2b58d05b7e1e67d116b771d2dee863e97445b350d780fae3
MD5 f155fcf7e6f74389203d87feed79ee26
BLAKE2b-256 1e4d4e1f1a2b24762bf31c80e89cee28ad1f64ed95d4f126c50b4412f28d9545

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2021.12.8-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 728c9a2f5d4ac76419ab25e914a34cf8ec1de37adeb6b0ed54177c7c946ac86f
MD5 a11b8f5eeb24d097969fa8e5f8c9e2bf
BLAKE2b-256 2c4f787e15bed758c2e067811a9a382548936e45ad948522ec2d7ebb30b8792f

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pytype-2021.12.8-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 09f09cd78a20593de75028798615bc5ba27c50e161ed2ca8fe0f617c48488e6b
MD5 a9fae4f8458c8f0ffdcf3f55011c3579
BLAKE2b-256 2c6137e1fc39f164d2e967a07607540fc43a4c5f4c6cf506ff38a4b2ef4cd849

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: pytype-2021.12.8-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for pytype-2021.12.8-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 c33b68e3de15520dd571c0143c72d0d93ecec1379d8470be1ed46ab8dd6e68ad
MD5 c2a549fe14f681df93a6de82bd31ea3e
BLAKE2b-256 ced7ce973a032eaa7b38b28b6cbc14cf858eca2f9357fa8255f355271ded20fa

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2021.12.8-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 207e140a02acca7b2372b6d948e3c5e12d15f448a4e106ce48902290e7c290be
MD5 de4f1f661946e70dac8c80b2dc56df4f
BLAKE2b-256 671e999edaeafc956cde0d708c7178f8b1bdeb8a4320a7b2297d79b167dc5de0

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pytype-2021.12.8-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 940891d8e52feb2f84bcb3ade07ac3a83044f450750de7091855cb63bc7e4c10
MD5 01f77b7be853e84052931636b6b841e6
BLAKE2b-256 e3caa0458cc385e80a50d82261b0d5f079b1ec1407066c472d92565a0fb18903

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: pytype-2021.12.8-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for pytype-2021.12.8-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 455c7d4f1e532905887c8e1cbf5a0619759254ff13ca3a692ad989417a7a1bcb
MD5 f803355b3ff8122807cdf42033b48617
BLAKE2b-256 e1f5ac746634fbe0e867bc90e9105880e5f2bf19d6aab22e59f710854371037c

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2021.12.8-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 765905af299da56f7223e24588727e1db68949e227f4de52ab51ae6070bba1b9
MD5 40aa89021d8f3bbdbd258f84b411e524
BLAKE2b-256 f4c6476129bd57d131f3dd6b4e4b2d570237ec5f23b6fa2a8172744faae928b6

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pytype-2021.12.8-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 715e38e37bdbbf5701703c8cc94b6b0de8eed4237cd5452d4920a5170092bd0a
MD5 c2bcbf206d6600f45b73df8f74c51da3
BLAKE2b-256 84797665327ac0dbae3348ab6c7440ad92040da4d3578e74fd7375b130180aba

See more details on using hashes here.

File details

Details for the file pytype-2021.12.8-cp36-cp36m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: pytype-2021.12.8-cp36-cp36m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.6m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for pytype-2021.12.8-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 40fe568ef4e0a7f6f5a80452e819b8e87430e304d53724f82204c35f6d4c0f02
MD5 c214185edb1b48a8bfc1175ec9adbb52
BLAKE2b-256 b70b6818d422c532a7b6a471da5ef30c4f29bf2c132a01ce76485fc042c843a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page