Manjusaka

Manjusaka

About pyright

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 type 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 Hinting 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:

image

Currently, there are two main static checking tools:

  1. mypy, which is officially supported by Python and PEP 484.

  2. pytype, developed by Google.

mypy currently has the following issues:

  1. Poor performance.

  2. Conservative approach towards adopting new features.

As a result, Google has chosen to introduce its own static checking solution, pytype, which offers significant improvements in performance and usability compared to mypy.

Today, Microsoft, known as the "Open Source Vanguard," has also released its own static checking tool, pyright. It claims to have a 5x performance improvement compared to mypy while ensuring compatibility with Type Hinting features.

This is a great advantage for large projects' CI.

However, there are potential risks and issues with pyright:

  1. It is developed based on TypeScript and runs on the node environment, which may pose difficulties for CI integration.

  2. Usability and reliability are still lacking.

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

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.