To quickly get started with type-checking a file or directory, run thefollowing, replacing file_or_directory with your input:
pip install pytypepytype file_or_directory
To set up pytype on an entire package, add the following to a setup.cfg filein the directory immediately above the package, replacing package_name withthe package name:
[pytype]inputs = package_name
Now you can run the no-argument command pytype to type-check the package. It'salso easy to add pytype to your automated testing; see thisexample of a GitHub project that runs pytype on Travis.
Finally, pytype generates files of inferred type information, located by defaultin pytype_output/pyi. You can use this information to type-annotate thecorresponding source file, replacing module.py with the file's import path:
Instead of using --recurse-submodules, you could also have run
git submodule initgit submodule update
in the pytype directory.
```usage: pytype [options] input [input ...]
positional arguments: input file or directory to process```
-V, --python-version: Python version (major.minor) of the target code.Defaults to 3.6.
-o, --output: The directory into which all pytype output goes, includinggenerated .pyi files. Defaults to pytype_output.
-d, --disable. Comma separated list of error names to ignore. Detailedexplanations 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 typeshedinstallation instead of its own bundled copy by setting $TYPESHED_HOME.
For convenience, you can save your pytype configuration in a file. The configfile is an INI-style file with a [pytype] section; if an explicit config fileis not supplied, pytype will look for a [pytype] section in the firstsetup.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 youneed. Directories may be relative to the location of the config file, which isuseful 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 toanalyze package ~/repo1/foo, which depends on package ~/repo2/bar:
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 includethe package itself.
```$ cat ~/repo1/pytype.cfg
NOTE: All relative paths are relative to the location of this file.
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 separated list of error names to ignore.
disable = attribute-error```
We could've discovered that ~/repo2 needed to be added to the pythonpath byrunning pytype's broken dependency checker: