Home Backend Development Python Tutorial Sharing of several Python type checking tools

Sharing of several Python type checking tools

Mar 27, 2019 am 09:52 AM
python

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

See all articles