GitHub - facebook/pyrefly: A fast type checker and IDE for Python (original) (raw)

License: MIT

Pyrefly: A fast type checker and IDE for Python

Currently under active development with known issues. Please open an issue if you find bugs.

Pyrefly is a fast type checker for Python. It's designed to replace the existing Pyre type checker at Meta by the end of 2025. This README describes basic usage. See the Pyrefly website for full documentation and a tool for checking code.

Getting Started

Pyrefly aims to increase development velocity with IDE features and by checking your Python code.

Key Features:

Getting Involved

If you have questions or would like to report a bug, pleasecreate an issue.

See ourcontributing guidefor information on how to contribute to Pyrefly.

Choices

There are a number of choices when writing a Python type checker. We are taking inspiration from Pyre1,Pyright andMyPy. Some notable choices:

Design

There are many nuances of design that change on a regular basis. But the basic substrate on which the checker is built involves three steps:

  1. Figure out what each module exports. That requires solving all import *statements transitively.
  2. For each module in isolation, convert it to bindings, dealing with all statements and scope information (both static and flow).
  3. Solve those bindings, which may require the solutions of bindings in other modules.

If we encounter unknowable information (e.g. recursion) we use Type::Var to insert placeholders which are filled in later.

For each module, we solve the steps sequentially and completely. In particular, we do not try and solve a specific identifier first (likeRoslyn orTypeScript), and do not use fine-grained incrementality (like Rust Analyzerusing Salsa). Instead, we aim for raw performance and a simpler module-centric design - there's no need to solve a single binding in isolation if solving all bindings in a module is fast enough.

Example of bindings

Given the program:

1: x: int = 4 2: print(x)

We might produce the bindings:

Of note:

Example of Var

Given the program:

1: x = 1 2: while test(): 3: x = x 4: print(x)

We end up with the bindings:

The expression phi is the join point of the two values, e.g. phi(int, str)would be int | str. We skip the distinction between define and use, since it is not necessary for this example.

When solving x@3 we encounter recursion. Operationally: