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.10 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.10).

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

Uploaded Source

Built Distributions

pytype-2022.12.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pytype-2022.12.9-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

pytype-2022.12.9-cp310-cp310-macosx_10_15_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.10 macOS 10.15+ x86-64

pytype-2022.12.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pytype-2022.12.9-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

pytype-2022.12.9-cp39-cp39-macosx_10_15_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.9 macOS 10.15+ x86-64

pytype-2022.12.9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pytype-2022.12.9-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

pytype-2022.12.9-cp38-cp38-macosx_10_15_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

pytype-2022.12.9-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view details)

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

pytype-2022.12.9-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.3 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

pytype-2022.12.9-cp37-cp37m-macosx_10_15_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pytype-2022.12.9.tar.gz
Algorithm Hash digest
SHA256 4998bd2c1b5c0ba464d4a60be29f1198f63ff569998ca6dd3425134b65caa9a0
MD5 62d1e4b9700f31990dd2f477d0f3fb47
BLAKE2b-256 e3f1b4af2268113f25cd5d1c44b2eea5a512e8c872eacacb2148b2b1e33b41fb

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.12.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d29d8658efccb91a3494eecad6724c29da52e96b40d928fe46f9423223205ff5
MD5 0adaa3781eb4d2ead4499156d8f42053
BLAKE2b-256 915e4d255602b0f4809eafe389e94bc735ad21523abba9545b66a97a4955a38b

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.12.9-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 deb30164dfa4f2c33bf400ecbad7a7361e87ca7700523818fc007a0f9562c0f0
MD5 366985dc0f96f515993b3bf6d48452d7
BLAKE2b-256 c97b2ac96d86e6a5fb523235c247d370afc3c334e103e1f2f4bdde523e0ba4b1

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2022.12.9-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pytype-2022.12.9-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 0f8c6da02c7468748d9baa75ba8f599f4bdaf04de6ec0f59ff1c30dfa55970f0
MD5 747394b66d42434ac83fe8f58d318cde
BLAKE2b-256 3ec121d1b260856b6e932e936d0c09ea04d41a9084a863bd1526b54e9c775f0f

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.12.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c804e8aa41695fd8ba42b19f5677cac6b9977f06904177c7b3d7523c0770e99f
MD5 a0f0ced1e4d6f4feaa5f93aae47932c0
BLAKE2b-256 5fd04f470450ef66a99ade0e0b0ff3fa16d8b43e4823aba66b7d86fb90b1f922

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.12.9-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 db791dd843cabc5203fca201970b5556195575d6d4d84a121e1e96916f564440
MD5 c55f6dff2a08173e44bf72969ab9cdc0
BLAKE2b-256 d33c3e1291382ef017045bb8b3b22966d9e46cdd3f51ad673f58fb8aaa3ce74e

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.12.9-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 04b62dd2731b95e8924ab47822e2fc6d813120fa7a8d26d1afceebd738b556c4
MD5 b9902195c10be213f5e4f2b36bc34839
BLAKE2b-256 31c17a2cdc350e9c5099909f9cf250a37fdb844967e2ee4e9913f6c3af81c407

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.12.9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8bda9b23c12d411ad104192cc79d757e86c6a3a7b05b0277089e68ae70422aee
MD5 035af01b15fde9c2d730c5b2b4b7be7d
BLAKE2b-256 56cefe7c0fb7f48aa6f2357da25cddb510afe4079f9d3579b41fd6a1fa2589f8

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.12.9-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 591c27881623ff6b3b6585143ca980cdac279ee5d5b130c94757922241c0dc39
MD5 4f0cf45ee76e4c40571eff95d4dfefca
BLAKE2b-256 dbd49cf09153fa1a162b8728c557e16dad9a10c1acbd662407ee18d3c72c35ca

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.12.9-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 9224a288c7fb62c29b6646aba8b432141445e8f60fab2a947952fa42eb155f7c
MD5 df1bc5cf761466e38fac950934bdf559
BLAKE2b-256 f3bc6039b0c3d4deedb24e98ec15e04b336524ebb4770b49a79935c7f17c67d7

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.12.9-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 808efa79ed8f09bc36c0a2ae992992e6a5c193c11dd4388e43be676bc9db893c
MD5 1591be2ac9e2043024602a62428fb55a
BLAKE2b-256 19a7727d14c39dd68960a37b3a1c397cce2d968af584bdf2586344c840f13724

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.12.9-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9ca1f233c815e8e3023d47eb1be080aeec0f0a68a9113a9aac6e2b307d7ba0be
MD5 5fc3f812395b9cd5eb5aedbb74a92991
BLAKE2b-256 34235f83ede520e4c17b1eaaf64dd29658623cbae0ebbe0b1ac500e28c1c448c

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2022.12.9-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 d5c4d50dde2b022826494fd5c743d9b6b6540c79cff18c1544021ce4b1883abc
MD5 3d9f12618287aa98da82f8f607b50f5f
BLAKE2b-256 03b6080e6bd0175026bf07cae3c8475f1abd3029ec57e6e9039569348163a9c9

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