Convert Figma logo to code with AI

facebook logopyre-check

Performant type-checking for python.

6,795
433
6,795
161

Top Related Projects

13,105

Static Type Checker for Python

5,286

It's not just a linter that annoys you!

3,428

flake8 is a python tool that glues together pycodestyle, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code.

4,706

A static type analyzer for Python code

18,182

Optional static typing for Python

Documentation and issues for Pylance

Quick Overview

Pyre-check is a performant type checker for Python developed by Facebook. It aims to provide fast and accurate static type checking for large Python codebases, helping developers catch type-related errors early in the development process.

Pros

  • Fast performance, especially on large codebases
  • Incremental analysis for quick feedback during development
  • Integration with popular editors and IDEs
  • Extensible through custom plugins

Cons

  • Steeper learning curve compared to some other Python type checkers
  • May require additional setup and configuration for optimal use
  • Some features may be more tailored to Facebook's internal use cases
  • Documentation could be more comprehensive for advanced use cases

Code Examples

  1. Basic type checking:
def greet(name: str) -> str:
    return f"Hello, {name}!"

result = greet(42)  # Pyre will catch this type error
  1. Using generics:
from typing import List, TypeVar

T = TypeVar('T')

def first_element(items: List[T]) -> T:
    return items[0]

first_element([1, 2, 3])  # Inferred as int
first_element(["a", "b", "c"])  # Inferred as str
  1. Custom protocols:
from typing import Protocol

class Drawable(Protocol):
    def draw(self) -> None: ...

def render(obj: Drawable) -> None:
    obj.draw()

class Circle:
    def draw(self) -> None:
        print("Drawing a circle")

render(Circle())  # Type checks successfully

Getting Started

  1. Install Pyre:
pip install pyre-check
  1. Initialize Pyre in your project:
pyre init
  1. Run Pyre:
pyre check
  1. Add type annotations to your Python files and run Pyre again to check for type errors.

Competitor Comparisons

13,105

Static Type Checker for Python

Pros of Pyright

  • Faster performance, especially for large codebases
  • More comprehensive type inference capabilities
  • Better integration with Visual Studio Code

Cons of Pyright

  • Less extensive documentation compared to Pyre
  • Smaller community and ecosystem of plugins/extensions

Code Comparison

Pyright:

# Type checking is performed without additional annotations
def greet(name: str) -> str:
    return f"Hello, {name}!"

Pyre:

# Requires explicit type annotations
from typing import Callable

def greet(name: str) -> str:
    return f"Hello, {name}!"

Both tools aim to provide static type checking for Python, but Pyright generally offers better performance and more advanced type inference. However, Pyre has more extensive documentation and a larger community. Pyright is particularly well-suited for use with Visual Studio Code, while Pyre may be preferred in environments already using other Facebook tools. The code comparison shows that Pyright can often infer types without explicit annotations, whereas Pyre typically requires more explicit type declarations.

5,286

It's not just a linter that annoys you!

Pros of Pylint

  • More established and widely used in the Python community
  • Broader scope of checks, including coding standards and error detection
  • Highly customizable with extensive configuration options

Cons of Pylint

  • Can be slower on large codebases compared to Pyre
  • May produce more false positives, requiring fine-tuning of configuration
  • Less focus on type checking compared to Pyre's specialized approach

Code Comparison

Pylint example:

import math

def calculate_area(radius):
    return math.pi * radius ** 2

result = calculate_area("5")  # Pylint will flag this as a potential error

Pyre example:

def greet(name: str) -> str:
    return f"Hello, {name}!"

result = greet(42)  # Pyre will catch this type mismatch

Pylint excels at catching a wide range of issues, including potential bugs and style violations. Pyre, on the other hand, focuses more on static type checking and can catch type-related errors that Pylint might miss. While both tools are valuable for Python development, Pylint offers a more comprehensive linting experience, whereas Pyre provides stronger type inference and checking capabilities.

3,428

flake8 is a python tool that glues together pycodestyle, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code.

Pros of flake8

  • Lightweight and fast, with minimal setup required
  • Highly customizable through plugins and configuration options
  • Widely adopted in the Python community, with extensive documentation

Cons of flake8

  • Limited type checking capabilities compared to Pyre
  • Focuses primarily on style and syntax issues, not deep semantic analysis
  • May require additional tools for more comprehensive code analysis

Code Comparison

flake8:

import sys

def example_function():
    unused_variable = 42
    print("Hello, World!")

example_function()

Pyre:

from typing import List

def example_function(numbers: List[int]) -> int:
    return sum(numbers)

result = example_function([1, 2, 3])
print(result)

flake8 would flag the unused variable and potentially suggest using a more specific import. Pyre, on the other hand, provides type checking and can detect type-related issues in the function signature and usage.

4,706

A static type analyzer for Python code

Pros of pytype

  • Supports gradual typing, allowing for incremental adoption in large codebases
  • Performs type inference, reducing the need for explicit type annotations
  • Integrates well with popular IDEs and text editors

Cons of pytype

  • Slower performance compared to Pyre, especially on larger codebases
  • Less comprehensive documentation and community support
  • Limited support for some advanced Python features and third-party libraries

Code Comparison

pytype:

def greet(name: str) -> str:
    return f"Hello, {name}!"

result = greet(42)  # pytype will catch this error

Pyre:

def greet(name: str) -> str:
    return f"Hello, {name}!"

result = greet(42)  # Pyre will also catch this error

Both tools can detect type errors in this example, but Pyre generally offers faster analysis and more advanced features for large-scale projects.

18,182

Optional static typing for Python

Pros of mypy

  • More mature and widely adopted in the Python community
  • Extensive documentation and large ecosystem of type stubs
  • Supports gradual typing, allowing incremental adoption

Cons of mypy

  • Generally slower performance compared to Pyre
  • Less advanced features for large-scale codebases
  • Limited support for advanced type inference in some cases

Code Comparison

mypy:

def greet(name: str) -> str:
    return f"Hello, {name}!"

result: str = greet("World")

Pyre:

def greet(name: str) -> str:
    return f"Hello, {name}!"

result: str = greet("World")

The basic syntax for type annotations is identical between mypy and Pyre. However, Pyre may offer more advanced features for complex codebases, such as:

# Pyre-specific example (not supported in mypy)
from pyre_extensions import safe_cast

def process_data(data: Any) -> None:
    if isinstance(data, dict):
        typed_data = safe_cast(Dict[str, int], data)
        # Now `typed_data` is known to be Dict[str, int]

Both tools serve similar purposes, but Pyre may have advantages in large-scale projects or those requiring more advanced type checking features.

Documentation and issues for Pylance

Pros of Pylance

  • Tighter integration with Visual Studio Code, providing a smoother user experience
  • Faster performance for large codebases, with quicker analysis and feedback
  • More comprehensive language server features, including better auto-completion and type inference

Cons of Pylance

  • Closed-source nature limits community contributions and customization
  • Less focus on static analysis compared to Pyre, which may result in missing some complex type errors

Code Comparison

Pylance (type checking):

def greet(name: str) -> str:
    return f"Hello, {name}!"

result = greet(42)  # Pylance will flag this as an error

Pyre (type checking):

def greet(name: str) -> str:
    return f"Hello, {name}!"

result = greet(42)  # Pyre will also flag this as an error

Both tools provide similar type checking capabilities in this basic example. However, Pyre may offer more advanced static analysis features for complex codebases, while Pylance excels in IDE integration and performance.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

lint tests pyre License: MIT Gitter

Pyre is a performant type checker for Python compliant with PEP 484. Pyre can analyze codebases with millions of lines of code incrementally – providing instantaneous feedback to developers as they write code. You can try it out on examples in the Pyre Playground.

Pyre ships with Pysa, a security focused static analysis tool we've built on top of Pyre that reasons about data flows in Python applications. Please refer to our documentation to get started with our security analysis.

Pysa is also available on the GitHub Marketplace as a Github Action

Requirements

To get started, you need Python 3.8 or later and watchman working on your system. On MacOS you can get everything with homebrew:

$ brew install python3 watchman

On Ubuntu, Mint, or Debian; use apt-get and homebrew:

$ sudo apt-get install python3 python3-pip python3-venv
$ brew install watchman

We tested Pyre on Ubuntu 18.04.5 LTS, CentOS 7, as well as OSX 10.11 and later.

Setting up a Project

We start by creating an empty project directory and setting up a virtual environment:

$ mkdir my_project && cd my_project
$ python3 -m venv ~/.venvs/venv
$ source ~/.venvs/venv/bin/activate
(venv) $ pip install pyre-check

Next, we teach Pyre about our new project:

(venv) $ pyre init

This command will set up a configuration for Pyre (.pyre_configuration) as well as watchman (.watchmanconfig) in your project's directory. Accept the defaults for now – you can change them later if necessary.

Running Pyre

We are now ready to run Pyre:

(venv) $ echo "i: int = 'string'" > test.py
(venv) $ pyre
 ƛ Found 1 type error!
test.py:1:0 Incompatible variable type [9]: i is declared to have type `int` but is used as type `str`.

This first invocation will start a daemon listening for filesystem changes – type checking your project incrementally as you make edits to the code. You will notice that subsequent invocations of pyre will be faster than the first one.

For more detailed documentation, see https://pyre-check.org.

Join the Pyre community

See CONTRIBUTING.md for how to help out.

License

Pyre is licensed under the MIT license.