Pyre-check
Pyre-check is a performant static type checker for Python, developed by Meta (formerly Facebook). It is compliant with PEP 484 and designed to analyze large codebases incrementally, providing quick feedback. It operates with a client-server architecture and includes Pysa, a security-focused static analysis tool. The library is actively maintained with regular updates.
Common errors
-
ImportError: cannot import name 'expand_global_root' from 'pyre_check.client.filesystem'
cause This error typically indicates an internal version mismatch or a breaking change in `pyre-check`'s internal client-server communication after an update, especially if local client files are out of sync with a running daemon or a newly installed binary.fixEnsure `pyre-check` is fully updated (`pip install --upgrade pyre-check`), restart any running Pyre daemon (`pyre stop` then `pyre start`), and if issues persist, try reinstalling in a fresh virtual environment. -
ƛ No watchman binary found. To enable pyre incremental, you can install watchman
cause The `watchman` utility, a file watching service developed by Meta, is not installed or not discoverable in your system's PATH. Pyre relies on `watchman` for efficient, incremental type checking.fixInstall `watchman` using your system's package manager (e.g., `brew install watchman` on macOS, `sudo apt-get install watchman` on Ubuntu/Debian). Ensure `watchman` is in your system's PATH. -
ƛ Incompatible variable type [9]: i is declared to have type `int` but is used as type `str`.
cause This is a fundamental type error detected by Pyre, indicating a value is being assigned or used with a type that does not match its declared annotation.fixCorrect the type annotation or the value assignment to ensure type consistency. For instance, if `i: int = 'string'` is the error, change it to `i: int = 123` or `i: str = 'string'`. -
ERROR: Could not find a version that satisfies the requirement pyre-check (from versions: none)
cause This usually means your Python version does not meet the `pyre-check`'s `requires_python` specification, or there's a problem with your `pip` environment or PyPI access.fixEnsure you are using Python 3.8 or newer (`python3 --version`). If your Python version is compatible, try updating `pip` and `setuptools` (`pip install --upgrade pip setuptools`) and clearing pip's cache.
Warnings
- gotcha Pyre-check strongly recommends installing and configuring `watchman` for optimal performance. Without `watchman`, Pyre defaults to non-incremental checks, which are significantly slower, especially on large codebases.
- gotcha Meta (the maintainer of Pyre-check) has announced and is promoting `Pyrefly` as its 'next-generation Python typechecker and language server'. While `pyre-check` is still active, this indicates a potential future shift in focus or eventual deprecation of `pyre-check` in favor of `Pyrefly`.
- gotcha Pyre uses a 'gradual typing' approach by default, meaning it often treats unannotated code or types marked `Any` as implicitly correct. To enforce stricter type checking, which catches more potential issues (e.g., missing annotations or `Any` types), enable 'strict mode' in your `.pyre_configuration` or per file with `# pyre-strict`.
- breaking Upgrading Pyre-check can sometimes surface new type errors due to improved analysis or changes in how types are interpreted. To manage this during upgrades, Meta provides `pyre-upgrade` to automatically suppress newly introduced errors, allowing for gradual resolution.
Install
-
pip install pyre-check
Quickstart
import os
project_name = "my_pyre_project"
os.system(f"mkdir {project_name} && cd {project_name}")
# Simulate project directory
os.chdir(project_name)
# Setup virtual environment and install pyre-check
os.system("python3 -m venv .venv")
os.system("source .venv/bin/activate") # In a real shell, this would activate the venv
os.system("pip install pyre-check")
# Initialize Pyre configuration
os.system("pyre init --no-watchman-comments --no-touch-pysa") # simplified for quickstart
# Create a Python file with a type error
with open("test_file.py", "w") as f:
f.write("""
def greet(name: str) -> str:
return f"Hello, {name}"
i: int = 'string' # This will cause a type error
print(greet(42)) # This will also cause a type error
""")
# Run Pyre to check for type errors
print("\n--- Running Pyre Check ---")
os.system("pyre")
# In a real environment, you'd deactivate with 'deactivate'
# os.system("deactivate")