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.7-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.7-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.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.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.9

# 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.

2022 Roadmap

  • Complete Python 3.9 feature support.
  • Basic Python 3.10 support.

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-2022.6.23.tar.gz (2.0 MB view details)

Uploaded Source

Built Distributions

pytype-2022.6.23-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pytype-2022.6.23-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.2 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

pytype-2022.6.23-cp310-cp310-macosx_10_15_universal2.whl (3.2 MB view details)

Uploaded CPython 3.10 macOS 10.15+ universal2 (ARM64, x86-64)

pytype-2022.6.23-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pytype-2022.6.23-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

pytype-2022.6.23-cp39-cp39-macosx_10_15_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.9 macOS 10.15+ x86-64

pytype-2022.6.23-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pytype-2022.6.23-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.2 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

pytype-2022.6.23-cp38-cp38-macosx_10_15_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

pytype-2022.6.23-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

pytype-2022.6.23-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.2 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

pytype-2022.6.23-cp37-cp37m-macosx_10_15_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: pytype-2022.6.23.tar.gz
  • Upload date:
  • Size: 2.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.12

File hashes

Hashes for pytype-2022.6.23.tar.gz
Algorithm Hash digest
SHA256 99d218c3b8de15d56c2dd8cc249d12727ced1d2c420ea2c73f53c5bb153c6821
MD5 6754e849c43e994b31d1d52418739699
BLAKE2b-256 15aabefcce369db44bd5d600bb4734cf4a6454987c1132d655bc1cbaaa43e8ec

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2022.6.23-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2022.6.23-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ed610b5a7bd32b0eed95fe64c251deba5cd43b93aa4f06fd1c96ada6eb9d7bea
MD5 088eff60c889591e05bf0e7c32d9345a
BLAKE2b-256 9a08eae6ec38acbdfc6f0feb5dc65959cc48fd6472134d74f485fed061ee31c6

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2022.6.23-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pytype-2022.6.23-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1b9876941e540f0b3d0460b0bf64908dc8050d41ebb9094823b0e3f9f5aefd4f
MD5 8d36c2c429207c2857aa0aaa56d099d5
BLAKE2b-256 be1ad0531a3b32d2cce8d33813d796901eaf295fc9a2cdfc107f1b01551eec87

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2022.6.23-cp310-cp310-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for pytype-2022.6.23-cp310-cp310-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 01e757a2683f3e33c70e7efe9a47038022e669d04bd1be42339ac9f8470b10c5
MD5 1086d883fb359fdd0f7a39f807cadfce
BLAKE2b-256 c46f5054c356e2779d3bc527d20e801f5104f59656f98c45b603c193fa946c7e

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.6.23-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2aca9227c15aa1efbcf1c216bb6f13ccfcd9881de44c4794bef58716df45ec21
MD5 f19fec72f09d3979afe571b47d44b533
BLAKE2b-256 5108e2547f2735975828c7495ce8ab68b24e69a67b7e1a62504245b224d0e053

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.6.23-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a5fda226f4b8471a73944af9af259933c0d7d48aa8740bb347dc2c4b36c912f8
MD5 d89c796c166ed8e897da164214ebc02a
BLAKE2b-256 6a25baba66d36e35b60319e1e2f48db0140618e015dce6a4bb754fa5132d06e8

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2022.6.23-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2022.6.23-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 cc2f0037f50bca68108f426e687405a1426964e7afc31100f4f4ddc5cd95cc1b
MD5 bb37d02eb9286fd08011c0410704ac38
BLAKE2b-256 94b7c85983baae321df25f117c73a51b2d774d50126c7e690e746e4337a1556d

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.6.23-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 69e7bddce6e86741692192debf4acc872260527074f71b373e52704a9caeec31
MD5 6de7cdbc8bb792e6e46e8971f43018c8
BLAKE2b-256 f18b5a94c59ee024c0b33591830b0e92cf29ede4e701be226b3e4690a5779a63

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.6.23-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7d2f5ae429ff9af9495ec4a1874cd5031e409862f7fdcac312baca2af2935c76
MD5 824a507d69916f17ebc628f8978deff1
BLAKE2b-256 c4f2c80e1cd6fac45dd080b694fad98f8bf168ec99491ac5ccb87b736b7eb636

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2022.6.23-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2022.6.23-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 ae128211ae23f629406b6f6ae1234b793376bf9d545d99d42d2a240d71c79ca2
MD5 4bc29411497e68c4ac5147755575a9b5
BLAKE2b-256 02dad8adede971c0e6ff989b4e426c5908d90dcaf7eaf66a89abddd9d4299b88

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.6.23-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8b47f5682c41bd176138e1984e7fa3ebe25fa31ffc1acca14e67a67e2ec0250d
MD5 70d929da4c59fac8dd91a81d397efee4
BLAKE2b-256 c2660955029e2d815a972e3eae4d69b0c27676351c0a707036b07292463e4cd1

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.6.23-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fab1e03db9b8df3c3e8743f43578cc95b7533799a01ec813c0367adc6b7601d5
MD5 2d3b255f0ceea77118d24bacbf30a938
BLAKE2b-256 8e58da3cd9eae94f968a114707eed293d07e1e796ff19c675992dbb739d092ad

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2022.6.23-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2022.6.23-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 951548b21f5e13e57e6fa1f2c33e9ff9a5f2093651a8a46f86081778ba43e1ea
MD5 c97ab48e5bdf6723a2c4abac3748687c
BLAKE2b-256 1db3f232796b7a0d008d0238fab7ba923c999115bf274e19616ffaf76b4b1db4

See more details on using hashes here.

Provenance

Supported by

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