Pytype checks and infers the type of Python code:
-
Use lint to check pure Python code, flag common errors such as misspelled property names, incorrect function calls, and much more, and it can even span files.
-
Reinforces user-supplied type annotations. Although annotations are optional for pytype, it will check and apply them when they exist.
-
Generating type annotations in a standalone file ("pyi file") can be merged back into the Python source code using the merge-pyi tool provided by itself.
Pytype is a static parser, which means it does not execute the code it is checking.
Google's thousands of projects rely on pytype to keep their Python code well-typed and error-free.
Quick Start
To quickly start type checking a file or directory, you can simply run the following command, replacing file_or_directory with your input:
To set the pytype on the entire package, add the following to the setup.cfg file in the package's parent directory, replacing package_name with the package name:
You can now run the no-argument command pytype to type-check packages. Adding pytype to your automated tests is also easy; see the GitHub project running pytype on Travis for an example.
Finally, pytype generates files containing the inferred type information, located by default in .pytype/pyi. You can use this information to type annotate the corresponding source files, replacing module.py with the import path to the file:
Demand
You will need a Python 2.7 or 3.5+ interpreter to run pytype, and also a Python interpreter in $PATH that is the same version of Python as the code being analyzed.
Platform Support:
-
Pytype is currently developed and tested on Linux, which is the main supported platform.
-
Installation on MacOSX requires OSX 10.7 or later, and Xcode v8 or later.
-
Windows is not currently supported.
Installation
Pytype can be installed via pip. Note that the installation process requires the use of wheel and setuptools. (If you are working in virtualenv, these two packages should already exist.)
Or install from the source on GitHub :
Instead of installing with the --recurse-submodules parameter, you can also install with the following command in the pytype directory:
Usage
Common Options:
-
-V,--Python-Version:Python version of the target code (major.minor). The default is 3.6.
-
-o,--output:The directory where all pytype output is saved, including the generated .pyi files. The default is .pytype.
-
-d,--disable。A comma-separated list of error names to ignore. pytype error names are described in detail in this document. The default is empty.
For a full list of options, run pytype --help.
In addition to the above, you can also run the pytype command directly to use a custom typeshed installation instead of its own bound copy by setting $TYPESHED_HOME.
Configuration file
For convenience, you can save your pytype configuration in a single file. The configuration file is an INI-style file with a [pytype] section; if no explicit configuration file is provided, pytype will traverse up from the current directory to find the [pytype] section in the first setup.cfg file.
Let's start by generating a sample configuration file:
Now we customize the file according to the local settings, keeping only the required parts. The directory may be a relative path to the configuration file, which is very useful if you want to check the configuration file as part of your project.
For example, suppose you have the following directory structure and want to analyze the package ~/repo1/foo, which depends on the package ~/repo2/bar:
The following configuration file is set up to instruct pytype to type check ~/repo1/foo as Python 3.6 code, look for packages in ~/repo1 and ~/repo2, and ignore attribute errors. Note that the path to the package does not include the package itself.
We may find that we need to add ~/repo2 to the pythonpath by running pytype's corrupt dependency checker:
Sub-tools
In addition to pytype itself, Pytype comes with three scripts:
-
merge-pyi, Used to merge type information from .pyi files into Python files.
-
pytd, A .pyi file parser.
-
pytype-single, A debugging tool for pytype developers to analyze a single Python file and assume that a .pyi file has been generated for all dependencies of that Python file.