What is Pyright and Why You Should Use It
Python is known for being dynamic, fast to prototype with, and easy to write. But these same characteristics can introduce bugs that are only caught at runtime.
Static type checking—validating code without running it—helps you catch bugs earlier. This is where Pyright enters the stage.
What is Pyright?
Pyright is a fast type checker for Python, built and maintained by Microsoft. It’s written in TypeScript and optimized for speed, developer experience, and VS Code integration.
It parses your Python code, inspects your type hints (like int
, str
, Optional
, etc.), and alerts you to any mismatches or unsafe code paths — before you even run your code.
Why Use Pyright?
Before asking this question - read my article about shift-left paradigm here
- Ultra-fast performance – Pyright is blazingly fast, even on large codebases.
- IDE integration – It powers Pylance, the default Python extension in Visual Studio Code, offering rich intellisense and inline errors.
- Strict type checking – You can gradually increase strictness (great for large, legacy codebases).
- Standalone CLI support – Pyright can be run without any IDE via the command line.
- Zero dependencies – It doesn’t require Python to be installed!
Installing Pyright
You can install Pyright globally with npm
:
npm install -g pyright
If you use VS Code and have the Pylance extension enabled, Pyright is already working under the hood.
Basic Usage
To check the current directory for type errors:
pyright
To check a specific file:
pyright my_script.py
You can also create a configuration file called pyrightconfig.json:
{
"include": ["src"],
"exclude": ["tests"],
"strict": true
}
Example 1: Catching Type Errors
def greet(name: str) -> str:
return "Hello " + name
greet(123) # Pyright will raise an error: Argument of type "int" is not assignable to parameter of type "str"
Example 2: Optional and None Handling
from typing import Optional
def length(s: Optional[str]) -> int:
return len(s) # Pyright error: Object of type "Optional[str]" has no attribute "len"
Fix:
def length(s: Optional[str]) -> int:
if s is None:
return 0
return len(s)
Example 3: Using TypedDict for JSON-like Structures
from typing import TypedDict
class User(TypedDict):
id: int
username: str
def print_user(user: User) -> None:
print(user["username"])
print_user({"id": 1, "username": "serhii_hrekov"})
If you accidentally pass in a dict without a username, Pyright will tell you right away.
Pyright vs Mypy
Feature | Pyright | Mypy |
---|---|---|
Performance | Extremely fast | Slower |
VS Code Integration | Excellent (Pylance) | Basic |
Configurability | High | Medium |
Language Server | Yes | No |
Active Development | Microsoft-backed | Community-backed |
Use Pyright if you:
- Work in VS Code
- Need instant feedback as you code
- Care about performance in big projects
Use Mypy if you:
- Prefer a mature ecosystem
- Need deep support for advanced plugins
Gradual Typing and Strict Mode
Pyright allows you to slowly adopt type safety:
{
"strict": true,
"reportMissingTypeStubs": false,
"reportOptionalSubscript": true
}
This makes it ideal for shift-left practices — catching errors as early as possible in development.
Further Reading
- Pyright GitHub Repository
- Pylance (VS Code extension)
- PEP 484 – Type Hints
- PEP 561 – Distributing and Packaging Type Information
- Shift-left testing explained
- Mypy vs Pyright — Reddit Thread
Final Thoughts
Pyright makes Python safer, smarter, and faster to write. It gives you real-time, IDE-level feedback and encourages clean API design. Whether you’re starting a new project or tightening up an old one, Pyright is an excellent addition to your developer toolkit.
Add type hints, run Pyright, and sleep better at night. LOL. 🛡️