Pyright for Python

1.1.408 · active · verified Sun Mar 29

Pyright for Python is a command-line wrapper that simplifies the installation and execution of Microsoft's Pyright static type checker within a Python environment. Pyright itself is a fast, standards-compliant type checker written in TypeScript and executed via Node.js. This wrapper handles the complexities of downloading and managing Node.js and the Pyright npm package, allowing Python developers to use Pyright without needing a separate Node.js installation. The project is actively maintained with frequent releases, typically aligned with upstream Pyright updates.

Warnings

Install

Quickstart

This quickstart demonstrates how to run Pyright on a simple Python file. The `pyright` Python package primarily provides a command-line executable. Programmatic invocation within Python is typically done by calling the `pyright` command via `subprocess`.

import os

# Create a dummy Python file to check
with open("my_module.py", "w") as f:
    f.write("def greet(name: str) -> int:\n    return f'Hello, {name}'\n\nresult = greet('World')\n")

# Run pyright via the command line (typical usage)
# This is equivalent to `pyright my_module.py` in the terminal
import subprocess

try:
    # Using python -m pyright to ensure the installed package's executable is found
    process = subprocess.run(
        ['python', '-m', 'pyright', 'my_module.py'],
        capture_output=True,
        text=True,
        check=False  # Do not raise an exception for non-zero exit codes
    )
    print("Pyright Output:\n" + process.stdout)
    if process.stderr:
        print("Pyright Errors:\n" + process.stderr)
    if process.returncode != 0:
        print(f"Pyright exited with code {process.returncode}. Errors found.")
    else:
        print("Pyright completed with no errors.")
except FileNotFoundError:
    print("Error: 'pyright' command not found. Ensure pyright is installed and in PATH.")
except Exception as e:
    print(f"An error occurred: {e}")

# Clean up the dummy file
os.remove("my_module.py")

view raw JSON →