Appendix: Type Checkers#
This placeholder text was generated by ChatGPT 4.5. It will be significantly rewritten as the book is developed. You can add comments to either https://bsky.app/bruceeckel or Github Issues.
Getting Started with mypy: Installation and Basic Use#
mypy is a popular static type checker for Python that validates type annotations:
Installation#
Basic Use#
Run mypy on your script or module to identify type errors:
Example:
# script.py
def greet(name: str) -> str:
return f"Hello, {name}!"
# Incorrect type:
print(greet(123)) # type: ignore
## Hello, 123!
Running mypy script.py outputs:
Advanced Configuration and Fine-tuning of mypy#
Customize mypy behavior using a mypy.ini or pyproject.toml file:
Example mypy.ini#
Example pyproject.toml#
Advanced configuration allows precise control over type-checking behavior and strictness levels.
Exploring pyright and IDE Integration (VSCode, PyCharm)#
pyright, developed by Microsoft, offers high-performance static type checking, integrated seamlessly with popular IDEs.
Using pyright (CLI)#
Install globally using npm:
VSCode Integration#
pyright powers VSCode's built-in Python type checking, providing immediate feedback:
- Install the Python extension in VSCode.
- Real-time inline error highlighting and suggestions.
PyCharm Integration#
PyCharm supports built-in type checking:
- Provides live error detection, highlighting, and quick-fix suggestions.
- Navigate to
Preferences > Editor > Inspectionsto configure type-checking rules.
Incremental Typing Strategies: Gradual Adoption#
Adopt typing gradually, focusing first on critical paths:
- Annotate high-risk or frequently changing modules first.
- Enable type checking incrementally to avoid overwhelming your team:
Incremental typing balances immediate benefits with manageable adoption efforts.
Integrating Type Checking into Continuous Integration#
Automate type checking within your CI/CD pipeline to enforce consistency and catch errors early:
Example GitHub Actions Workflow#
name: Type Check
on: [ push, pull_request ]
jobs:
type-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
with:
python-version: "3.11"
- run: pip install mypy
- run: mypy .
Automated checks ensure ongoing compliance with typing standards and maintain high code quality.
Handling and Resolving Common Errors#
Common type-checking errors and their resolutions:
Incorrect Type Annotation#
# example_2.py
# Error: Incompatible types in assignment
# (expression has type "int", variable has type "str")
name: str = 123 # type: ignore
Missing Imports#
Use ignore_missing_imports or install type stubs:
Or install stubs:
Union and Optional Errors#
Resolve ambiguity clearly:
# example_3.py
from typing import Optional
# Error: Incompatible return value type (got "None", expected "int")
def find_index(item: str, items: list[str]) -> Optional[int]:
try:
return items.index(item)
except ValueError:
return None
Effectively handling and resolving these errors leads to clearer, more reliable, and maintainable codebases.