pytype 2024.10.11


pip install pytype

  Latest version

Released: Oct 11, 2024


Meta
Maintainer: Google
Requires Python: >=3.10

Classifiers

Development Status
  • 5 - Production/Stable

Intended Audience
  • Developers

License
  • OSI Approved :: Apache Software License

Programming Language
  • Python
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: Implementation :: CPython

Topic
  • Software Development

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

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.

License

Apache 2.0

Disclaimer

This is not an official Google product.

2024.10.11 Oct 11, 2024
2024.9.13 Sep 13, 2024
2024.4.11 Apr 12, 2024
2024.3.19 Mar 19, 2024
2024.3.11 Mar 12, 2024
2024.2.27 Feb 27, 2024
2024.2.13 Feb 13, 2024
2024.2.9 Feb 10, 2024
2024.1.24 Jan 25, 2024
2024.1.5 Jan 05, 2024
2023.12.18 Dec 18, 2023
2023.12.8 Dec 08, 2023
2023.12.7 Dec 07, 2023
2023.11.29 Nov 30, 2023
2023.11.21 Nov 22, 2023
2023.10.31 Oct 31, 2023
2023.10.24 Oct 24, 2023
2023.10.17 Oct 17, 2023
2023.10.5 Oct 05, 2023
2023.9.27 Sep 27, 2023
2023.9.19 Sep 19, 2023
2023.9.11 Sep 11, 2023
2023.8.31 Aug 31, 2023
2023.8.22 Aug 22, 2023
2023.8.14 Aug 15, 2023
2023.7.28 Jul 28, 2023
2023.7.21 Jul 21, 2023
2023.7.12 Jul 12, 2023
2023.6.16 Jun 17, 2023
2023.6.2 Jun 02, 2023
2023.5.24 May 25, 2023
2023.5.8 May 09, 2023
2023.4.27 Apr 27, 2023
2023.4.18 Apr 18, 2023
2023.4.11 Apr 11, 2023
2023.3.31 Mar 31, 2023
2023.3.13 Mar 14, 2023
2023.3.8 Mar 09, 2023
2023.3.2 Mar 03, 2023
2023.2.17 Feb 17, 2023
2023.2.14 Feb 14, 2023
2023.2.9 Feb 10, 2023
2023.1.31 Jan 31, 2023
2023.1.17 Jan 18, 2023
2023.1.10 Jan 10, 2023
2022.12.15 Dec 15, 2022
2022.12.9 Dec 09, 2022
2022.11.29 Nov 29, 2022
2022.11.18 Nov 19, 2022
2022.11.10 Nov 11, 2022
2022.10.26 Oct 26, 2022
2022.10.13 Oct 13, 2022
2022.9.27 Sep 27, 2022
2022.9.19 Sep 20, 2022
2022.9.8 Sep 08, 2022
2022.8.30 Aug 30, 2022
2022.8.23 Aug 23, 2022
2022.8.17 Aug 18, 2022
2022.8.3 Aug 03, 2022
2022.7.26 Jul 26, 2022
2022.7.18 Jul 19, 2022
2022.6.30 Jun 30, 2022
2022.6.23 Jun 23, 2022
2022.6.14 Jun 14, 2022
2022.6.6 Jun 07, 2022
2022.5.19 May 19, 2022
2022.5.10 May 10, 2022
2022.5.5 May 05, 2022
2022.4.26 Apr 26, 2022
2022.4.22 Apr 22, 2022
2022.4.15 Apr 15, 2022
2022.4.6 Apr 06, 2022
2022.3.29 Mar 30, 2022
2022.3.21 Mar 22, 2022
2022.3.8 Mar 08, 2022
2022.2.23 Feb 24, 2022
2022.2.17 Feb 17, 2022
2022.2.8 Feb 08, 2022
2022.1.31 Jan 31, 2022
2022.1.13 Jan 13, 2022
2022.1.7 Jan 07, 2022
2022.1.5 Jan 05, 2022
2021.12.15 Dec 15, 2021
2021.12.8 Dec 08, 2021
2021.11.29 Nov 30, 2021
2021.11.24 Nov 24, 2021
2021.11.18 Nov 19, 2021
2021.11.12 Nov 12, 2021
2021.11.2 Nov 02, 2021
2021.10.25 Oct 25, 2021
2021.10.18 Oct 18, 2021
2021.10.11 Oct 11, 2021
2021.10.4 Oct 04, 2021
2021.9.27 Sep 27, 2021
2021.9.9 Sep 09, 2021
2021.8.24 Aug 24, 2021
2021.8.11 Aug 12, 2021
2021.8.3 Aug 03, 2021
2021.7.27 Jul 27, 2021
2021.7.19 Jul 19, 2021
2021.6.17 Jun 17, 2021
2021.5.25 May 25, 2021
2021.5.19 May 19, 2021
2021.5.14 May 14, 2021
2021.5.11 May 11, 2021
2021.5.6 May 06, 2021
2021.5.4 May 04, 2021
2021.4.26 Apr 27, 2021
2021.4.15 Apr 16, 2021
2021.4.9 Apr 09, 2021
2021.4.1 Apr 01, 2021
2021.3.22 Mar 23, 2021
2021.3.10 Mar 11, 2021
2021.3.3 Mar 04, 2021
2021.2.23 Feb 23, 2021
2021.2.19 Feb 20, 2021
2021.2.9 Feb 09, 2021
2021.1.28 Jan 29, 2021
2021.1.21 Jan 22, 2021
2021.1.14 Jan 15, 2021
2021.1.8 Jan 09, 2021
2020.12.23 Dec 23, 2020
2020.12.16 Dec 16, 2020
2020.12.2 Dec 03, 2020
2020.11.23 Nov 24, 2020
2020.11.12 Nov 13, 2020
2020.11.3 Nov 05, 2020
2020.10.8 Oct 08, 2020
2020.9.29 Sep 29, 2020
2020.9.24 Sep 24, 2020
2020.9.16 Sep 17, 2020
2020.9.14 Sep 14, 2020
2020.8.28 Aug 28, 2020
2020.8.17 Aug 18, 2020
2020.8.10 Aug 10, 2020
2020.7.30 Jul 31, 2020
2020.7.24 Jul 24, 2020
2020.7.20 Jul 21, 2020
2020.7.14 Jul 15, 2020
2020.6.26 Jun 26, 2020
2020.6.1 Jun 01, 2020
2020.5.13 May 13, 2020
2020.5.7 May 07, 2020
2020.4.22 Apr 23, 2020
2020.4.1 Apr 02, 2020
2020.3.19 Mar 19, 2020
2020.2.20 Feb 21, 2020
2020.2.6 Feb 06, 2020
2020.1.24 Jan 24, 2020
2020.1.8 Jan 09, 2020
2020.1.7 Jan 07, 2020
2019.12.17 Dec 17, 2019
2019.12.6 Dec 06, 2019
2019.11.27 Nov 27, 2019
2019.10.17 Oct 17, 2019
2019.9.17 Sep 17, 2019
2019.9.6 Sep 06, 2019
2019.8.29 Aug 29, 2019
2019.8.9 Aug 10, 2019
2019.7.30 Jul 30, 2019
2019.7.26 Jul 26, 2019
2019.7.11 Jul 11, 2019
2019.6.28 Jun 28, 2019
2019.6.21 Jun 22, 2019
2019.5.31 May 31, 2019
2019.5.24 May 24, 2019
2019.5.15 May 15, 2019
2019.5.8 May 09, 2019
2019.5.6 May 06, 2019
2019.4.26 Apr 26, 2019
2019.4.19 Apr 19, 2019
2019.4.12 Apr 12, 2019
2019.4.5 Apr 05, 2019
2019.4.2.1 Apr 03, 2019
2019.4.2 Apr 02, 2019
2019.3.27 Mar 27, 2019
2019.3.21 Mar 21, 2019
2019.3.15 Mar 16, 2019
2019.3.8 Mar 08, 2019
2019.3.1 Mar 02, 2019
2019.2.13 Feb 14, 2019
2019.1.30 Jan 30, 2019
2019.1.18 Jan 19, 2019
2018.12.21 Dec 26, 2018
2018.12.11 Dec 12, 2018
2018.11.6 Nov 07, 2018
2018.10.30 Oct 31, 2018
2018.9.26 Sep 26, 2018
2018.9.25 Sep 25, 2018
2018.9.19 Sep 20, 2018
2018.9.18 Sep 18, 2018
2018.9.7.1 Sep 08, 2018
2018.9.7 Sep 07, 2018
2018.8.10 Aug 10, 2018
2018.7.13 Aug 09, 2018
2018.6.19 Jun 20, 2018
2018.6.18 Jun 19, 2018
2018.6.15 Jun 15, 2018
2018.6.5 Jun 05, 2018
2018.5.22.1 May 22, 2018
2018.5.22 May 22, 2018
2018.5.15 May 15, 2018
2018.5.14 May 15, 2018
Extras: None
Dependencies:
attrs (>=21.4.0)
importlab (>=0.8)
immutabledict (>=4.1.0)
jinja2 (>=3.1.2)
libcst (>=1.0.1)
msgspec (>=0.18.6)
networkx (>=2.8)
ninja (>=1.10.0.post2)
pycnite (>=2024.07.31)
pydot (>=1.4.2)
tabulate (>=0.8.10)
toml (>=0.10.2)
typing-extensions (>=4.3.0)