Annotate Python with type hints and check it with mypy

8 steps · about 2 h 50 min · Intermediate

  1. Read the original type hints proposal (article, Python PEPs, 30 min)

    Explains why annotations exist and the rules every checker follows.

  2. Run mypy on your first file (article, mypy docs, 20 min)

    Shows how to install mypy and read its first error messages.

  3. Keep the type hints cheat sheet nearby (article, mypy docs, 15 min)

    A compact reference for common annotations you will write daily.

  4. Write generic functions and classes (article, mypy docs, 25 min)

    Teaches TypeVar and generic containers so helpers keep precise types.

  5. Describe behaviour with protocols (article, mypy docs, 25 min)

    Shows structural typing so you can type duck-typed code without inheritance.

  6. Fix the most common mypy errors (article, mypy docs, 20 min)

    Prepares you for Optional, None checks and untyped-definition complaints.

  7. Introduce types into an existing codebase (article, mypy docs, 20 min)

    Gives a gradual plan for typing a project that already runs.

  8. Configure strictness in mypy.ini (article, mypy docs, 15 min)

    Lets you lock in your progress with per-module strictness settings.