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

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

Uploaded Source

Built Distributions

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

pytype-2023.11.21-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

pytype-2023.11.21-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

pytype-2023.11.21-cp311-cp311-macosx_10_9_universal2.whl (4.2 MB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)

pytype-2023.11.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

pytype-2023.11.21-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64

pytype-2023.11.21-cp310-cp310-macosx_11_0_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.10macOS 11.0+ x86-64

pytype-2023.11.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

pytype-2023.11.21-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ ARM64

pytype-2023.11.21-cp39-cp39-macosx_11_0_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.9macOS 11.0+ x86-64

pytype-2023.11.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

pytype-2023.11.21-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.2 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ ARM64

pytype-2023.11.21-cp38-cp38-macosx_11_0_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.8macOS 11.0+ x86-64

File details

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

File metadata

  • Download URL: pytype-2023.11.21.tar.gz
  • Upload date:
  • Size: 2.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for pytype-2023.11.21.tar.gz
Algorithm Hash digest
SHA256 7ee403f21e78a3b7583c576e8d41df49a826dc7fe5f1de3457a27f151a0fd1fc
MD5 6113fa0e18633158a719af626b9d7ace
BLAKE2b-256 296488b21347afe93a5d5e93994c90c2e5d5763f5ea96c68f6df60f43c36b0fa

See more details on using hashes here.

File details

Details for the file pytype-2023.11.21-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2023.11.21-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2181bdbccf13543d9357c2dde569d37eb72e06cb2832d06608e9fbb8bd7dd3f4
MD5 26dbbd08ebcb5fb535039951eb20189b
BLAKE2b-256 1c34586354872737787f5605778fb03b82e1487fcb18e8c29cc22194008b1cfc

See more details on using hashes here.

File details

Details for the file pytype-2023.11.21-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pytype-2023.11.21-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b8789cf0dafe513e7e0c581548a41729491e0b5c793bfccfe040b9ed590e0f65
MD5 4531d44fc42aa3aefac5dfa911f664d9
BLAKE2b-256 bc872d4870202b63a8e7efaa976344f70756c3b6f33f989230b817f40d4ae553

See more details on using hashes here.

File details

Details for the file pytype-2023.11.21-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pytype-2023.11.21-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 86a06eb8ede9614e2ad1096b7bb2c880124db8c0fb7a3ae6c32e34bf058097b2
MD5 63f4978761a7910e94dcf2182e8d00a4
BLAKE2b-256 a9a4fba1f5b1221d3437694a041bcdcca97679aec1021316107b848117134046

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytype-2023.11.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dd5699d064d6cc76533f04af4b5c7051efd1f8d61dd44d71c9d8e58e2ffcc168
MD5 d04315d676c30685f8de5ef3da7f5334
BLAKE2b-256 6cf98ed5935f65d5583575776febfb1b2c99ce290072657dc8c1365b2e0e6856

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytype-2023.11.21-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fb1844af7f10e392dfe0a0173a3296445bf6f1b1674823bd9208f65dcd9ad7ec
MD5 5d064ab94dacc93c3c7a7eb7797560b3
BLAKE2b-256 1b4934b55039013585a5451b5272083a449a9e3b18fac0f0a27e681e52180c38

See more details on using hashes here.

File details

Details for the file pytype-2023.11.21-cp310-cp310-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2023.11.21-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 bac6a03b10d45b2b1d9641efba2d8b9e8ef23cc8398ceca953738b1207a5d2ca
MD5 c1690fdb7e4f68b1e61decfd51973aac
BLAKE2b-256 c223b1588df0cdb74462fba4a38c8c47f96191ec005c976052942679c9cdb0fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytype-2023.11.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 53c33bf751fb6244fc2edd5b88575f94c4cb5425cc03057c48b463508f083083
MD5 22a2f44c616f3b8335bbb76de97ff5c0
BLAKE2b-256 0cb71e2ccdfdb8f41cdb4ae9c187c4a70c626bc0c3034fec56bead4019b77b33

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytype-2023.11.21-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a8ce2ed40602db8dda706829f0a6b2d845ef08a5da3e683675d955762d020f03
MD5 a7969c7f113618e1bc564e40b4fb3e0a
BLAKE2b-256 db47893805f9e0deb3abb9407fcb89a87bcb4da93ce95189dd68eb44b43dcac6

See more details on using hashes here.

File details

Details for the file pytype-2023.11.21-cp39-cp39-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2023.11.21-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 ada66121d45bff95facf8b00acdf390d5303fb606f44966ff7765754181b0d78
MD5 2ea74c73f9b1821dd16b5d597e8f0af1
BLAKE2b-256 a90294521074ad2df26687f01ffb8e891efffb1d5baf8c1b4dba919c3204fa54

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytype-2023.11.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5bb1e3262a6fff60334be0128529877b335197424400d56cf7510fd98671f6f9
MD5 8f8b6daa4eb870d9c65477cc6605ad91
BLAKE2b-256 5ebd8202ee4660d16c3ef7bcbf70027376cd4c426f8f56e91bcb295c301ef665

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytype-2023.11.21-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 769b475c44f63a8f912725d65f0ec4dd2cbce294300b2220fff5bb5f12e1bd26
MD5 abc7a330687b9d73a314c0dddffb7f7d
BLAKE2b-256 7b67e9639618f21380a8a916040efcf9fa69afff8cce7b0b4905ad04cebe4731

See more details on using hashes here.

File details

Details for the file pytype-2023.11.21-cp38-cp38-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2023.11.21-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 efd35b530ec371cae4f4d9613e54b51d990e140d176c0929b86bef04f5c73cd2
MD5 6a9cc9e99e436dbdda6aeeea08a1d717
BLAKE2b-256 2e4c688ba3ea998c37250673400259d8b697b1a56f31e4f12e68a37cf9c19388

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