mypy - Static Type Checking for Python
Overview
mypy is the standard static type checker for Python, enabling gradual typing with type hints (PEP 484) and comprehensive type safety. It catches type errors before runtime, improves code documentation, and enhances IDE support while maintaining Python's dynamic nature through incremental adoption.
Key Features:
- Gradual typing: Add types incrementally to existing code
- Strict mode: Maximum type safety with --strict flag
- Type inference: Automatically infer types from context
- Protocol support: Structural typing (duck typing with types)
- Generic types: TypeVar, Generic, and advanced type patterns
- Framework integration: FastAPI, Django, Pydantic compatibility
- Plugin system: Extend type checking for libraries
- Incremental checking: Fast type checking on large codebases
Installation:
Type Annotation Basics
1. Variable Type Hints
2. Function Type Hints
3. Collection Type Hints
4. Class Type Hints
Advanced Type Hints
1. Literal Types
2. Type Aliases
3. Generics and TypeVar
4. Protocol (Structural Typing)
5. Callable Types
mypy Configuration
1. mypy.ini Configuration
2. pyproject.toml Configuration
3. Strict Mode
Incremental Adoption Strategies
1. Start with Entry Points
2. Per-Module Strict Mode
3. Use # type: ignore Strategically
4. Reveal Types During Development
FastAPI Integration
1. FastAPI with Type Hints
2. Async FastAPI Type Checking
3. FastAPI Dependency Injection Types
Django Integration
1. Django with django-stubs
2. Django Models with Type Hints
3. Django Views with Type Hints
Type Stubs and Third-Party Libraries
1. Installing Type Stubs
2. Creating Custom Stubs
3. Ignoring Missing Imports
CI/CD Integration
1. GitHub Actions
2. Pre-commit Hook
3. Make Target
Common Patterns and Idioms
1. Overload for Multiple Signatures
2. TypedDict for Structured Dicts
3. Final and Constant Values
4. Self Type for Method Chaining
mypy vs pyright Comparison
Feature Comparison
When to Use mypy
When to Use pyright
Using Both
Local mypy Profiles (Your Repos)
Common patterns from your Python projects:
- Strict default (edgar, kuzu-memory, mcp-browser):
disallow_untyped_defs = true,check_untyped_defs = true,no_implicit_optional = true,warn_return_any = true,strict_equality = true. - Relaxed profile (mcp-ticketer): strict flags disabled temporarily with a
disable_error_codelist for patch releases. - Incremental adoption (mcp-vector-search):
ignore_errors = truewhile stabilizing types. - Missing imports:
ignore_missing_imports = trueused in mcp-memory and mcp-ticketer.
Reference: see pyproject.toml in edgar, kuzu-memory, mcp-vector-search, and mcp-ticketer.
Best Practices
1. Start with Key Modules
2. Use Type Aliases for Readability
3. Prefer Explicit Over Implicit
4. Use reveal_type for Debugging
5. Document Type Ignores
Common Pitfalls
❌ Anti-Pattern 1: Using Any Everywhere
Correct:
❌ Anti-Pattern 2: Ignoring Type Errors Globally
Correct:
❌ Anti-Pattern 3: Not Using Optional
Correct:
Quick Reference
Common Commands
Error Code Reference
Resources
- Official Documentation: https://mypy.readthedocs.io/
- Type Hints PEP: https://peps.python.org/pep-0484/
- typing Module: https://docs.python.org/3/library/typing.html
- mypy GitHub: https://github.com/python/mypy
- Type Stubs: https://github.com/python/typeshed
- django-stubs: https://github.com/typeddjango/django-stubs
- FastAPI + mypy: https://fastapi.tiangolo.com/tutorial/type-hints/
Related Skills
When using mypy, consider these complementary skills (available in the skill library):
- pytest: Type-safe testing with mypy - integrates type checking into your test suite for comprehensive type coverage
- fastapi-local-dev: FastAPI with full type safety - combines FastAPI's runtime validation with mypy's static checking
- pydantic: Runtime type validation with mypy support - validates data at runtime while mypy validates at compile time
mypy Version Compatibility: This skill covers mypy 1.8+ and reflects current best practices for Python type checking in 2025.


