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

Uploaded Source

Built Distributions

pytype-2024.2.13-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pytype-2024.2.13-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

pytype-2024.2.13-cp311-cp311-macosx_10_9_universal2.whl (4.3 MB view details)

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

pytype-2024.2.13-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pytype-2024.2.13-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

pytype-2024.2.13-cp310-cp310-macosx_11_0_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.10 macOS 11.0+ x86-64

pytype-2024.2.13-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pytype-2024.2.13-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

pytype-2024.2.13-cp39-cp39-macosx_11_0_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.9 macOS 11.0+ x86-64

pytype-2024.2.13-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pytype-2024.2.13-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

pytype-2024.2.13-cp38-cp38-macosx_11_0_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.8 macOS 11.0+ x86-64

File details

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

File metadata

  • Download URL: pytype-2024.2.13.tar.gz
  • Upload date:
  • Size: 2.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for pytype-2024.2.13.tar.gz
Algorithm Hash digest
SHA256 5a2bf755b6a62c3f4c2ae7a5102c8da04011d94646a4faf3c6bcc3b44fbb0ced
MD5 1a650cdf9fd11c23e6327a774b4d7dca
BLAKE2b-256 9e7a9ab6ee2588c1bd114bfe7d96015f90c9d11b3224d6a9915f35e7a481a495

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 108fbd615e2ed97ab7d7895d4e60c703af0534f60fc23826daa6b5c516155777
MD5 20bac112af82c7bfcd9bc96e030e6ff8
BLAKE2b-256 839e6f47f68048a8b102b4e9ef51e94d00c75f5dfebd29a0d0174e309fbe416a

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e3c6c097ae2654db35c8c09b58dd63092fb7d5791ab937903e93bc2f6aa65c59
MD5 4b8f62a276c8310f0af7f5ab87864e71
BLAKE2b-256 b972bf84d68b7878824d485dbab38dc14a9e94edd3f8e90a19d970e2ce9c7372

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 5c739230fb52fcc4a52f46396627982ade99a349edccecc4dfd572c22279a382
MD5 9d682c8f4127dd908292bdf9525b7bbc
BLAKE2b-256 f7dc92391b25a09abbe9a96045e23b552cd3f567939157087bc1d59574eacdcc

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dad172615419a21c6b2f37a8a4ff85d3d46d317137844f6b2721b732a415ef38
MD5 5ba2ee62b4406ffb3d93080efbd57bdc
BLAKE2b-256 bef33727037a53ef738ce4124556b8c57c975eee84d10a17b9a0df5d49d2c607

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a3fc8ecda755e041b78e80966bc000eb5cc491b2912dfa3d20022b1ed5d205ec
MD5 14ef2cd6f292ae8a191b082b34f9d415
BLAKE2b-256 14cb53ca77b1e6087c1c484782fae3eeaced63a494825689683b782a92050a70

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 0842b4e5611d1c2533e2c4f432bfe94483632f7b27eb5081573f90d8b939c1c3
MD5 e9d9f4374305d7703119ef8b9c3b7bae
BLAKE2b-256 962f109389d134252a8113b5c957cdbd6e218cc9bcb36fb2aab7c77377685a2b

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6186ad48824a26b23ce2d5fb7f874c29bed1fbf655cb4e4342eec623d9fd7192
MD5 43257bfd58abfd514930bdc4c8b03d2a
BLAKE2b-256 949e6f4c30e2bb57a578ef6f08fc8a1c1e7554aaeece911b66771af129ce9298

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2a707c4480ca909301fc98d5e69486eec8f7a6020ea2885ee9112d94d8b9aca1
MD5 d7c42cbe42fb369c479f582ef8f2e863
BLAKE2b-256 91d8757bd57595dfd50f33c7a6b29530ec2ca0025bc2fbafd9d5ea3fe72a5821

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 b543d1c8435e5ecad1341619b5de0fbc5e96999db8b427c779719c9615035ca4
MD5 d7d6aa48f479854ed6827886970abaa1
BLAKE2b-256 0713b46bb050babd43e4b369ec5907b1e779b4eec6b74fe4bcef2c7e68889df1

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c3fe3cbc5b20e719b95b3cc84a8a631994ebf78d5fabb43f624d2c8ca1d0fc3f
MD5 1f2c6dedfda7100b61186037f8cb12df
BLAKE2b-256 d50193ebf063c8901b686f77a3ade9b3ec2a80302e443a05a80cc76d70c1b10f

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 42074f97ab46a76d77c160364a62ca95c5d58bb5f3ba45cb2e65b01b3663801b
MD5 fe7ec5576570af7e82b47ce647fdd32b
BLAKE2b-256 d7d8391f5eaf10dfaf4a19d57c4f0f0aa672c70a765aa3fab95db7d06d1d2feb

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.13-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 b11bdeb8b8137e0aa3de71d77d2085a17733e29c3b9edf70aa4194fccb2e762d
MD5 6557896bec5c392bb5c00291181aaee9
BLAKE2b-256 b71058d65dc503fd8a2199929d21705a3e1d970ac64a53c68dd96874f4610240

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