0Pricing
Python Academy · Lesson

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.py

Your 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

  1. Basic Type Annotations
  2. Complex Types: List, Dict, Optional, Union
  3. TypeVar, Generic Classes, and Protocol
  4. Running mypy and Fixing Type Errors
← Back to Python Academy