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:

Pytype

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:

Pytype

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:

Pytype

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

Pytype

Or install from the source on GitHub :

Pytype

Instead of installing with the --recurse-submodules parameter, you can also install with the following command in the pytype directory:

Pytype

Usage

Pytype

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:

Pytype

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:

Pytype

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.

Pytype

We may find that we need to add ~/repo2 to the pythonpath by running pytype's corrupt dependency checker:

Pytype

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.

Related articles

Encrypting files with Python

In life, sometimes we need to encrypt some important files,Python provides easy-to-use cryptographic libraries such as hashlib, base64, etc.

Quickly test your Python code with Hypothesis

Testing is important no matter which programming language or framework you use. hypothesis is an advanced testing library for Python. It allows writing test cases with parameters, and then generating easy-to-understand test data that makes the test fail.

mplcyberpunk tutorials

Cyberpunk 2077" is a very popular single-player game ~ The game is set in the year 2077, a highly developed technology but chaotic and disorderly "cyberpunk" city. In this world, although the technology is highly developed, but the standard of living of

Heartrate: Real-time Visualization for Python Execution

Heartate - Track programs like a heart rate,Heartrate is a Python tool library that allows you to visualize the execution of Python programs in real time. Monitoring a running Python program is shown in the following figure.

Implementing reverse proxies with Django only

When you think of reverse proxies, you say nginx. nginx is the ideal reverse proxy tool. But now the conditions are tough. The server doesn't have nginx and doesn't have root privileges, which means you can't compile and install nginx, and only one port,

What does join mean in python?

Python has two functions .join() and os.path.join(), which do the following: . join(): Concatenate string arrays. Concatenates the elements of a string, a tuple, and a list with the specified characters (separator) to generate a new string os.path.join()

Add new content to the python dict

Adding new content to a dictionary is done by adding new key/value pairs, modifying or deleting existing key/value pairs as in the following examples

How to delete elements in python dict

clear() method is used to clear all the data in the dictionary, because it is an in-place operation, so it returns None (also can be interpreted as no return value)

python crawler how to get cookies

Cookie, refers to the data (usually encrypted) that some websites store on the user's local terminal in order to identify the user and perform session tracking. For example, some websites require login to access a page, before login, you want to grab the