Sharing of several Python type checking tools

不言
Release: 2019-03-27 09:52:48
forward
1827 people have browsed it

This article brings you the sharing of several Python type checking tools. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Recently, Microsoft has open sourced a Python static type checking tool on Github: pyright, which has attracted much attention in the community.

Microsoft’s participation in open source projects is getting bigger and bigger. Not to mention the big strategic ambition of acquiring Github, its open source VS Code editor has already attracted countless fans in the ape world. , even Kenneth Reitz, a celebrity in our Python circle (the author of multiple open source projects, including requests, requests-html, responder, etc.), is full of praise for it.

Pyright, which is now open source, has a good reputation, so let’s take a look at its capabilities and introduce several other type checking tools by the way.

As we all know, Python is a dynamically typed language, and the actual type of variables is not known until runtime. This is the characteristic of dynamic languages. However, in teamwork or large-scale projects, the cost of maintenance is inevitable. As the saying goes: " Dynamics are exciting for a while, but reconstruction is a crematorium".

As early as PEP-3107 in 2006, Python introduced the function annotation function, which was finally implemented in version 3.0. In version 3.5, Python continued to introduce static type checking syntax (ie, PEP-484, type hints). The 2014 PEP-483 even made a theoretical summary under the title "The Theory of Type Hints". Later, PEP-526 and PEP-544 were successively proposed, and the specifications for type checking were gradually enriched.

The benefit of type checking is to check early, detect type errors in advance, and enhance the consistency and maintainability of the code. (It also prevents hair loss, meow)

# 不加检查
def greeting(name):
    return 'Hello ' + name

# 添加检查
def greeting(name: str) -> str:
    return 'Hello ' + name
Copy after login

As shown in the above example, after adding checks, it can be determined at compile time whether the input parameters and return values ​​are of string type.

Before Microsoft launched pyright, there were three mainstream static checking tools: the official mypy, Google’s pytype, and Facebook’s pyre-check . The three-legged situation is about to be broken.

Sharing of several Python type checking tools

#pyright's documentation claims that it has the following characteristics:

  • Fast speed. Compared to mypy and other checking tools written in Python, it is 5 times or more faster.
  • Does not depend on Python environment. It is written in TypeScript, runs on node, and does not rely on the Python environment or third-party packages.
  • Highly configurable. Supports free configuration and supports specifying different running environments (PYTHONPATH settings, Python version, platform targets).
  • Check items are complete. Supports type checking and checking of other syntax items (such as PEP-484, PEP-526, PEP-544), as well as checking of function return values, class variables, global variables, and even conditional loop statements
  • commands line tools. It contains two VS Code plug-ins: a command line tool and a Language Server Protocol
  • Built-in Stubs. A copy of Typeshed is used. (Note: Use static pyi files to check built-in modules, standard libraries and third-party components)
  • Language service features. Hover prompt information, symbol definition jump, and real-time editing feedback

In this regard, it is not unpowerful. In fact, pyright "stands on the shoulders of giants", and its functions seem to be inherited from several other predecessors.

Let’s look at the official mypy, which was personally developed by Guido van Rossum, the “father of Python”. It is the most mainstream choice. It was launched early, has a large user base, and has the richest documentation and community experience.

In terms of integrated IDE, all mainstream editors support: PyCharm, Vim, Emacs, Sublime Text, VS Code, Atom... In terms of industry experience, Instagram and Dropbox projects started with py2 Migrating to py3 is to use it as a guarantee.

Then look at Google's pytype. According to the documentation, it can:

  • Mark common errors, such as spelling errors and function call errors
  • Enhance custom types Annotation
  • Supports generating type annotations for pyi files

Looking at the document, I found that it has a function that is quite user-friendly, namely "Error noise reduction" , for errors that do not need to be modified, comments can be added to eliminate type checking.

In addition, there is another good consideration. In order to write type checking, other modules may be introduced into the module. For the latter, pytype has a way to hide it and only load it when doing type checking.

Finally, I want to introduce Facebook’s pyre-check, which was open sourced last year and has received a lot of praise (maybe it was because of it that Microsoft launched the copyright project).

The basic functions are similar, but it also has its highlights. pyre-check can integrate the Watchman module. This "watcher" will monitor the code file and track the changes made. Microsoft's pright has a watch mode, which should have absorbed this and made it easier to use (because there is no need to install Watchman and other dependencies additionally).

Pyre-check also has a highlight. It has a query parameter, which can perform local and regional checks on the source code, such as querying the type of an expression in a certain line, querying all methods of a class and returning it as a list, etc. etc. This avoids the need for a comprehensive inspection.

After introducing the four type checking tools, here is a summary comparison:

Sharing of several Python type checking tools

As for their performance, is it really as Pyright said, its Is the speed 5 times that of the others? Interested students can try it. If you have any experience using it, please leave a message to communicate with me.

Project address:

https://github.com/python/mypy, https://github.com/Microsoft/pyright, https://github .com/google/pytype, https://github.com/facebook/pyre-check

This article has ended here. For more other exciting content, you can pay attention to the ## of the PHP Chinese website. #python video tutorial column!

The above is the detailed content of Sharing of several Python type checking tools. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!