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

Uploaded Source

Built Distributions

pytype-2024.2.27-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.27-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.27-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.27-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.27-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.27-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.27-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.27-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.27-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.27-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.27-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.27-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.27.tar.gz.

File metadata

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

File hashes

Hashes for pytype-2024.2.27.tar.gz
Algorithm Hash digest
SHA256 68cb0ecc3acee571f73d4f376c5c1a218771de039d18fd55eb903ee5d407f027
MD5 c5136afcda2bdd4741fad480701e3771
BLAKE2b-256 684f4e0cea3a74f7fda6e71ab8251a366e509b14f46009c45de0462caaa8f7b4

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7a7c3eb3fe72cfb6c4c70e7de82c906c80397fe54a8b24b756e07a1d995d6e79
MD5 1646e97cfd81ef719063cc88f94d9323
BLAKE2b-256 c194988fdfd9c72a3cac7886ea0ee09f210114a0eebeeaa206d3783979cd3a8b

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 36f00b6e2b6646a5573dbfbb3ff7e58ecf136cc721555366fef97a7f5631d983
MD5 aa3cf3fc066364ed20a4b99b219efe8c
BLAKE2b-256 963b16a97fbadc3daa5618bedd302af208e52174700e54113108397a230ffc33

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 ea833547b9df5b12b0f87a9382c11a62c8c1d5650a71cb1a3c6053815b555659
MD5 7db68433690a0ef2243e047e6cddb455
BLAKE2b-256 f304766a9979cc50d8ae5cf6b59f74c6c20902d5cc9635a86abc2ce0b29c230d

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3e0485a90b71baf894ead5072fa9cff77d40cafc92f6fd40f7ccdeac83aed5ed
MD5 1ca2e449067022007465e6ebfc141c93
BLAKE2b-256 3019e6317973ec48447a92ded62308925d6bafca73ef084fc7dccf3b4e48c46c

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ae69f272256ef76b4b66a18a40160df048b163e09f552d8e81d7833676fb5782
MD5 6d2de9c10076609d48c35bf62fe455c1
BLAKE2b-256 77758105450f2df73bb361d72da4676f1650540f2da57e1a0558de5c37fdd69f

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 a8bba18a6b80c238386d3b3d7054799bd6afb8d9d2c5f3b5ea3c5b13900b0adb
MD5 6229e86b47e260f7fac59fd8f2e3541e
BLAKE2b-256 c2b945030725cadb7086ea84eb3b11ed6ffa2ecd4c23dcc8318724a41f31755f

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b57689893fcd0ebbad8913d8e3ef90d77fb8b87e8845c86e5fe130d3dea0f432
MD5 1857a62076450e69ce213c4330b4c4d4
BLAKE2b-256 fc5419d634957e9fb704205c3067f63324cb1bca15798f950f1861a9b04ebe5c

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2604c209b71e567af865aed568659f7b13a01345138fa3c2df9e3154a3252216
MD5 901ac997940666fda5e11b2310c12726
BLAKE2b-256 29a2abb9784d4bdf3fd74e6d7acbc06bcb553bc91da577543879982ab9819a06

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 ac9d1f101308d293b6ea2670eae59de478b24156532665dfebce6a5b6af64214
MD5 60e140eaee1843f2f0ad876bcf47924e
BLAKE2b-256 033cbdec86aeee450793356e6f20e88a4aaab6399b7cbb3365bdceb41992d42c

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 012415bc9c446f107addc99a13e9b8eec9345eb00f65d8fe1c52375fbee44273
MD5 d50280e6a7ba561c55acf569242a1af3
BLAKE2b-256 b0be5d6333ab2a751c0da20ef3d231c5f83cc7e2681092904ac5253432ddf1b2

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8156210439e06e52f0479d4057298e47887133ca65a7d92918e8ac14a112fbfc
MD5 040633535c55fa3110b0be88af774dc8
BLAKE2b-256 3838e0870bb29049d24d79e5bc0c0e06798460eab34c0b78bcce5bfbf30d6bc8

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pytype-2024.2.27-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 d40dc7416b1bb2d1b30f3f4ae725eec74ca64d2cbdabc1bfafcfcda632212611
MD5 ceb17203a4808299fa83f003da09eda3
BLAKE2b-256 f6192b00ed2ca540605848b610daa57acaff3796051b1c9e7d6dd07d3a933b84

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