Skip to main content

Python type inferencer

Project description

Tests 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 or FAQ.

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

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

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.

* Note: On Alpine Linux, installing may fail due to issues with upstream dependencies. See the details of this issue for a possible fix.

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

2020 Roadmap

  • Python 3.7 and 3.8 support
  • Stricter type annotation enforcement
  • Better performance on large files
  • Developer documentation

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

Uploaded Source

Built Distributions

pytype-2020.9.29-cp38-cp38-manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.8

pytype-2020.9.29-cp38-cp38-macosx_10_14_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

pytype-2020.9.29-cp37-cp37m-manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.7m

pytype-2020.9.29-cp37-cp37m-macosx_10_14_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

pytype-2020.9.29-cp36-cp36m-manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.6m

pytype-2020.9.29-cp36-cp36m-macosx_10_14_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.6m macOS 10.14+ x86-64

pytype-2020.9.29-cp35-cp35m-manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.5m

pytype-2020.9.29-cp35-cp35m-macosx_10_14_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.5m macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: pytype-2020.9.29.tar.gz
  • Upload date:
  • Size: 1.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.7

File hashes

Hashes for pytype-2020.9.29.tar.gz
Algorithm Hash digest
SHA256 b44c0904160c6b55ee3f9e00227719a62bb4e3388ae54220da2e0609388325fe
MD5 01e11001bcaf7e76af3b8420acf672c8
BLAKE2b-256 012fb13afba5fdaea9402fbe78a82c373070fa5728a51da6bd3af01c32cff38f

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2020.9.29-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

  • Download URL: pytype-2020.9.29-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.7

File hashes

Hashes for pytype-2020.9.29-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d5550ad4e26794780561e91548ed11ef059ebfadf52aa09c0b61b05ba3a1488a
MD5 bcd055fbb117b58a8c2bf12dcbef617d
BLAKE2b-256 15e882f2f7ce70e42083a2d1eb907f0b3ebf2de17a4e8fbe159e01a5669e1489

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: pytype-2020.9.29-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.7

File hashes

Hashes for pytype-2020.9.29-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2849c861cc3b8a795356a68d9f144634e6e6f0a82ecf81842ba7ec16481bded0
MD5 a87774f501769c27b891ffeb324ec0fa
BLAKE2b-256 d53222da2a1be391bc990e65309887f5b7fd42d8e98504ded6120d8cfb5730b8

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2020.9.29-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: pytype-2020.9.29-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.7

File hashes

Hashes for pytype-2020.9.29-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cd781459c3000a3ffe54346471eb6e8ff1a29c2658d9d0aab43ea2ec4eebf12e
MD5 94a4f70651a84bab8522ed26f7bc159d
BLAKE2b-256 1ca1525619818aca35f5e4bcdbbc7064c18c27b07e2b571d7fdbbbc7f46a8f66

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: pytype-2020.9.29-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.7

File hashes

Hashes for pytype-2020.9.29-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 6aeb435f64fc3ab5fbd8a9997f64068e9c78e8d4691459cf46c75c6a7d560ccc
MD5 c998fc68cf36f77fe88a8948956294db
BLAKE2b-256 01f971308a217c08bc3ef9d2438d407da8ef9b2ac20255fbca91caaeeb4fa094

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2020.9.29-cp36-cp36m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: pytype-2020.9.29-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.7

File hashes

Hashes for pytype-2020.9.29-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e1dd3e387b850084305d2fb1c55b946d603b98b3bc0dbe57e8b7fe158f9e2cf6
MD5 59cd1b8ca7253bc40072e941d0f970cc
BLAKE2b-256 bbc3173711e50cfd2eb18bb3fdaa1615dba457100293a621689f4b3bdc265b77

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: pytype-2020.9.29-cp36-cp36m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.6m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.7

File hashes

Hashes for pytype-2020.9.29-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ceec03a974f3b728701a85e365a21e62a41143d95d0b099cc23bc490929bf85e
MD5 2e10809114964b582c0c1ced11cab2f5
BLAKE2b-256 aa1a417a3ce1939b5195af2008af3ccb4e2c56e175adf809208c3cb416f8b828

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2020.9.29-cp35-cp35m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: pytype-2020.9.29-cp35-cp35m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.7

File hashes

Hashes for pytype-2020.9.29-cp35-cp35m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fa53cf1ee3134368c4a330d75ebb9a84acf8126ea5ec102795087924cfcf5cec
MD5 d094f46d1d380a49686c436fb19b498b
BLAKE2b-256 aef5c00614983efac6e11f1d8c9e38a8b2301f6f11aa9fd076c89aec741fc444

See more details on using hashes here.

Provenance

File details

Details for the file pytype-2020.9.29-cp35-cp35m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: pytype-2020.9.29-cp35-cp35m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.5m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.7

File hashes

Hashes for pytype-2020.9.29-cp35-cp35m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 c424a17bd6d1a559291ad22cc2a91596ded5d8653665894d7a1cedf976303ff4
MD5 15a62d3e96496a9c5f45eb9e05f18bde
BLAKE2b-256 f9f9e20873a847545fd42533898282543fcc7f34c44628e56562df6b5a9e7201

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