| name | type-checking |
| description | Use when adding type hints, fixing type checker errors, expanding type coverage, or when user mentions "type check", "pyright", "basedpyright", "mypy", "type hints", "type errors", "type annotations", "typing", "Any type", "weak types". |
Type Checking with Pyright/Basedpyright
Use pyright or basedpyright for gradual type checking adoption.
Core Principles
- Minimize Logic Changes: Type checking should NOT change runtime behavior
- Test After Changes: Always run tests after adding type hints
- Hunt Down Root Causes: Never use
# type: ignore as first resort
Quick Start
When fixing type errors:
pyright <file-or-directory>
basedpyright <file-or-directory>
git diff main
pytest tests/
Fix Priority Order
- Add proper type annotations (Optional, specific types)
- Fix decorator return types
- Use
cast() for runtime-compatible but statically unverifiable types
- Last resort:
# type: ignore only for legitimate cases
Common Quick Fixes
Field Defaults
role: Role = Field(default=Role.MEMBER, description="...")
role: Role = Field(Role.MEMBER, description="...")
Optional Parameters
def my_function(channel_id: Optional[str] = None):
def my_function(channel_id: str = None):
Weak Types - NEVER Use
items: list[Any]
data: dict
result: Any
items: list[DataItem]
data: dict[str, ProcessedResult]
result: SpecificType | OtherType
Prefer cast() Over type: ignore
from typing import cast
typed_results = cast(list[ResultProtocol], results)
selected = select_by_score(typed_results)
selected = select_by_score(results)
When to Use type: ignore
Only for:
- Function attributes:
func._attr = val # type: ignore[attr-defined]
- Dynamic/runtime attributes not in type system
- External library quirks (protobuf, webhooks)
- Legacy patterns requiring significant refactoring
DO NOT use for simple fixes (add Optional, fix return types, add imports).
Reference Files
For detailed patterns and procedures:
Remember: Always verify changes with git diff main before committing.