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.10 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.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 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.9.11.tar.gz (2.9 MB view details)

Uploaded Source

Built Distributions

pytype-2023.9.11-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pytype-2023.9.11-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

pytype-2023.9.11-cp310-cp310-macosx_11_0_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.10 macOS 11.0+ x86-64

pytype-2023.9.11-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pytype-2023.9.11-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

pytype-2023.9.11-cp39-cp39-macosx_11_0_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.9 macOS 11.0+ x86-64

pytype-2023.9.11-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pytype-2023.9.11-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

pytype-2023.9.11-cp38-cp38-macosx_11_0_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.8 macOS 11.0+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pytype-2023.9.11.tar.gz
Algorithm Hash digest
SHA256 cb64b861346e51fb118e1dd0492c7bc7304ad1d53594353502ed9aed1f18089f
MD5 b091374af657c54931d39b50fe1444e2
BLAKE2b-256 452ac1e20a1a57efdb6f04fa9d079fba2ac91b5a6ed2e1e8ff6c688d8ff353af

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2023.9.11-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3235237af4f6a287245d7f55e25ae61e998b3c2cd000297322770934e8091083
MD5 a006aca407b54056f5a6d2079fddc829
BLAKE2b-256 1c7b8389c239c625418209808ea9665aeb0e8c2fce2c8317761ea67edcf704f1

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2023.9.11-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 becdc1b35abafd8a269884d120b2da73c0d840626568da975a05e706036a4131
MD5 a552aa79f6a6a5aba2d8565f4c59a3ec
BLAKE2b-256 87f7afb55203365af465df19101b6fb4efc8a408e3f93ccfbbdd994398ec5202

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2023.9.11-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 8614a5cf432d833da51483e239509b21ad20ce4bc02d6293b35aa73f5b01ec76
MD5 c900fcc260d356e97f7a2db76c8972f1
BLAKE2b-256 e5b58b975f19255b1e016f55be68cc0116245ca550547ec0d6c00d69f4f541d7

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2023.9.11-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d1e152a56c74e7479c2c13fe279e32c67464946ca07483e07e14973277e62529
MD5 0e482ecae735c5225749b28ca93b3b8c
BLAKE2b-256 18fe823d4285f1a8ca2eb97ff5c3699155a92e7f1308dc25ebd8583d2b16aa48

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2023.9.11-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 170ffe9f54feb3de8e9bdf60900fdad625b04e5705843367e2e52cfcb658fda9
MD5 fa48875b11fee7be96070ac34aa0a56b
BLAKE2b-256 f070eb420f3f17aaa4a4d145fe110451074277354d3649a3df5776b12ee91ab4

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2023.9.11-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 85a12bee6a771e98bc08ad578a2b817fb074b69b746fe478f2d3a11435d1002b
MD5 916b771ef7a6b2b6b7cbc0b9f96ee46a
BLAKE2b-256 b135a70701997f23bfd58697ba3d5ce551295760cf4a11375e675e548ee599cb

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2023.9.11-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e43c6bc64953aab0390d95aaa10852bba5d49484f5c49fdcf95b6ac2532905d4
MD5 552fab048ec948f9b30a816aa49e5c4c
BLAKE2b-256 c9a2326c362298b9086a55cfc73373b605ed450b0e6aa1892d5938a9db38231e

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2023.9.11-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 62486057e648dbf6c1297b92565c70c7b2301e9f141639014e874e58c6cd0b97
MD5 54af8f3b3e324930ed483f5b64272ca2
BLAKE2b-256 1bc1b8dea99aa3f9caadd5446ea95ada1a6a68d8566f8572709e8cc849b35a61

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2023.9.11-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 4b9295c32bcb721f5aa051eb50456409dc306ad482061b5e297d9bd6ab1e2724
MD5 64e76b2ddec3f2e0d39a37837ebe05d4
BLAKE2b-256 55cee979b8232224cc5b795b05595a12ec4510103084dcbb6333e8adba7ffd7f

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