Running mypy and Fixing Type Errors
Configure mypy, interpret errors, and incrementally type a codebase.
Installing and Running mypy
Install mypy with pip and run it on a file or package. It reports type errors without executing your code.
# pip install mypy
# Check a single file:
# mypy script.py
# Check a package:
# mypy mypackage/
# Strict mode (recommended for new code):
# mypy --strict script.pyYour First mypy Error
mypy detects incompatible types, missing return values, and un-annotated function arguments.
# script.py
def add(a, b): # mypy: Missing type annotation
return a + b
result: int = add("hello", 1) # str, not int
# mypy output:
# error: Returning Any from function declared to return "int"All lessons in this course
- Basic Type Annotations
- Complex Types: List, Dict, Optional, Union
- TypeVar, Generic Classes, and Protocol
- Running mypy and Fixing Type Errors