github.com-google-pytype_-_2019-03-21_22-06-36
Item Preview
Share or Embed This Item
Flag this item for
- Publication date
- 2015-03-18
git clone google-pytype_-_2019-03-21_22-06-36.bundle -b master
A static type analyzer for Python code
Pytype
Pytype checks and infers types for your Python code - without requiring typeannotations. Pytype can:
- Lint plain Python code, flagging common mistakes such as mispelled attributenames, incorrect function calls, and much more, even acrossfile boundaries.
- Enforce user-provided type annotations. While annotations areoptional 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 providedmerge-pyi tool.
Pytype is a static analyzer, meaning it does not execute the code it runs on.
Thousands of projects at Google rely on pytype to keep their Python codewell-typed and error-free.
For more information, check out the user guide or FAQ.
Quickstart
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:
merge-pyi -i module.py pytype_output/pyi/module.pyi
Requirements
You need a Python 2.7 or 3.5+ interpreter to run pytype, as well as aninterpreter in $PATH
for the Python version of the code you're analyzing.
Platform support:
- Pytype is currently developed and tested on Linux, which is the main supportedplatform.
- Installation on MacOSX requires OSX 10.7 or higher and Xcode v8 or higher.
- Windows is currently not supported.
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 shouldalready be present.)
pip install pytype
Or from the source code on GitHub.
git clone --recurse-submodules https://github.com/google/pytype.gitcd pytypepip install -U .
Instead of using --recurse-submodules
, you could also have run
git submodule initgit submodule update
in the pytype
directory.
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 to3.6
.-o, --output
: The directory into which all pytype output goes, includinggenerated .pyi files. Defaults topytype_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
.
Config File
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
:
~/├── 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.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.
[pytype]
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:
```$ pytype --config=~/repo1/pytype.cfg ~/repo1/foo/*.py --unresolved
Unresolved dependencies: bar.dependency```
Subtools
Pytype ships with three scripts in addition to pytype
itself:
merge-pyi
, for merging type information from a .pyi file into aPython file.pytd
, a parser for .pyi files.pytype-single
, a debugging tool for pytype developers, which analyzes asingle Python file assuming that .pyi files have already been generated for allof its dependencies.
Roadmap
- Windows support
License
Apache 2.0
Disclaimer
This is not an official Google product.
Source: https://github.com/google/pytype
Uploader: google
Upload date: 2015-03-18
- Addeddate
- 2019-03-24 18:24:08
- Identifier
- github.com-google-pytype_-_2019-03-21_22-06-36
- Originalurl
- https://github.com/google/pytype
- Pushed_date
- 2019-03-21 22:06:36
- Scanner
- Internet Archive Python library 1.7.7
- Uploaded_with
- iagitup - v1.0
- Year
- 2015