About pyright#
PEP 484, it has been almost four years since its release. Today I came across a new library and decided to write a short article to recommend and rant about it.
About PEP 484#
PEP 484 was formally proposed in 2014 and accepted in 2015, becoming a part of the standard for Python 3.5 and later versions. In short, it introduces static type checking to Python through additional syntax.
Here's a simple example:
def return_callback(flag: bool, callback: typing.Callable[[int, int], int])-> int:
if not flag:
return None
return callback(1, 2)
By using this type annotation, we can improve readability and enable static checking. For more details, you can refer to the slides from my presentation at BPUG last year. I will also take some time to discuss the history and significance of Type Hints in the near future (flag+1).
Static Checking#
The significance of static checking lies in its ability to detect low-level errors in a timely manner. With timely checks, it can be easily integrated into CI or Git Hooks.
Here's a simple example:
Currently, there are two main static checking tools:
mypy currently has the following issues:
-
Poor performance.
-
Conservative approach towards adopting new features.
As a result, Google has chosen to introduce its own static checking solution, pytype, which has significantly improved performance and usability compared to mypy.
Recently, Microsoft, known as the "open source pioneer," has also released its own static checking tool, pyright. It claims to have 5 times better performance than mypy while ensuring compatibility with Type Hint-related features.
This is a great advantage for large projects in terms of CI.
However, there are potential risks and issues with pyright:
-
It is developed based on TypeScript and runs on the node environment, which may pose difficulties for CI integration.
-
Usability and reliability are still lacking.
-
There may be a shortage of plugins for IDE editors (the official statement mentions that the VSCode plugin is still under development).
Nevertheless, it is still worth trying out pyright. In the future, I will also attempt to read and understand the implementation of pyright to explore Microsoft's approach (flag+2).
Well, that's all for this article. This is probably the most casual article I have ever written.