Microsoft has recently opened a Python static type checking tool called PyRight on Github, which has attracted a lot of attention in the community.
Microsoft is becoming more and more involved in open source projects. Despite its strategic ambition to acquire Github, Microsoft’s OPEN source VS Code editor has already gained a lot of fans in the simian world. Even Kenneth Reitz, the author of many open source projects, Requests, requests- HTML, responder, etc.) raved about it.
Pyright is open source and has a good reputation, so let’s take a look at what it can do and introduce some other type checking tools.
Python is known as a dynamically typed language, where the actual type of a variable is known at run time. This is the nature of dynamic languages, but when it comes to teamwork or large projects, maintenance comes at a cost, as the saying goes: “Dynamic is good for a while, refactoring the crematorium.”
Function annotations were introduced in Python as early as peP-3107 in 2006, and were eventually implemented in version 3.0. With version 3.5, Python continued to introduce static type checking syntax (peP-484, Type Hints). Pep-483 in 2014 even made a theoretical summary under The title of The Theory of Type Hints. Later, PEP-526 and PEP-544 were proposed successively, and the specification of type checking was gradually enriched.
The benefits of type checking are early detection, early detection of type errors, and increased code consistency and maintainability. (And prevent hair loss, meow)
# No checks
def greeting(name):
return 'Hello ' + name
# add check
def greeting(name: str) -> str:
return 'Hello ' + nameCopy the code
As shown in the example above, the addition of checks allows you to determine at compile time whether the input parameter and return value are strings.
Before Microsoft introduced PyRight, there were three main static checking tools: the official Mypy, Google’s PyType, and Facebook’s Pyre-Check. The tripartite situation is about to be broken.
Pyright’s documentation claims the following features:
-
Speed is fast. It is five times or more faster than Mypy and other checkers written in Python.
-
Independent of the Python environment. It is written in TypeScript, runs on Node, and does not rely on Python environments or third-party packages.
-
Strong configurability. Configuration is supported freely, allowing you to specify different runtime environments (PYTHONPATH Settings, Python versions, platform targets).
-
Check items are complete. Support for checking types and other syntax items (peP-484, PEP-526, PEP-544), function return values, class variables, global variables, and even conditional loop statements
-
Command line tools. It includes 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 tripartite widgets.)
-
Language service features. Hover prompts, jump to symbol definition, real-time editing feedback
In that regard, it is powerful. In fact, PyRight stands “on the shoulders of giants,” and its features seem to have been inherited from its predecessors.
Then there is the official MYPY, which was personally developed by “The father of Python” Guido van Rossum. It is the most mainstream choice, launched early, has a large user base, and has the most documentation and community experience.
In terms of integrated ides, all the major editors support PyCharm, Vim, Emacs, Sublime Text, VS Code, Atom…… In terms of industry experience, Instagram and Dropbox have used it to secure their projects from Py2 to Py3.
Moving on to Google’s PyType, which, according to the documentation, can:
-
Flag common errors, such as spelling errors, function call errors
-
Enhance custom type annotations
-
Support for generating type annotations to PYI files
Looking through the documentation, I found that it had a very user-friendly feature called “error denoising”, which eliminates type checking by adding comments for errors that don’t need to be fixed.
It is also good to consider that additional modules may be introduced to write type checking, for which PyType has a way of hiding it and loading it only for type checking.
Finally, There’s Facebook’s Pyre-Check, which was launched last year and received a lot of good reviews (maybe that’s why Microsoft launched PyRight).
The basic function points are pretty much the same, but there are some nice points. Pyre-check integrates with the Watchman module, an “observer” that listens to code files and tracks changes made. Microsoft’s PyRight has a Watch mode, which presumably absorbs this and is much more useful (since there is no need to install extra Watchman and other dependencies).
Another nice thing about Pyre-Check is that it has a query parameter that allows you to perform local checks on the source code, such as querying the type of an expression in a line, querying all the methods of a class and returning them to a list, and so on, to avoid a full check.
After the introduction of the four types of inspection tools, the following is a summary comparison:
As for their performance, is it really five times faster than the others, as Pyright claims? Those of you who are interested can try it. What is your experience in using it
Scan the following QR code to add the assistant customer service sister wechat, and immediately reply to receive python materials for free
Follow the wechat public account “Python Community Camp” to learn technical know-how in the first time