Gradual typing & mypy

P31.typing.01 · Audience: guest, it-ml, language-pro

Real LLM grading for this pageLLM grading (this page):

Python is dynamically typed, but annotations are gradual: you add them where they pay, they cost nothing at runtime, and a checker like mypy reads them to catch bugs before the code runs — this repo gates on mypy packages/compute. In the browser we can't run mypy, so these rungs grade the annotations by inspecting them (typing.get_type_hints) — the same information a checker reads.

Step 1 / 2An annotation is a contract
ⓘ Concept: Written down, checked, read by everyone
def f(x: int) -> str promises what goes in and what comes out. It has no runtime cost — Python ignores it — but mypy checks it statically, your editor autocompletes from it, and teammates rely on it. The annotation is documentation that cannot drift, because the checker enforces it.

Why it matters — A signature you can trust means you don't read the body — and mypy turns a whole class of bugs into a red build instead of a 2am incident.

Ask the mentor about this module

Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.

Ctrl/Cmd + Enter to send

🎓 Practice ladder

2 graded rungs · ~20 min

Annotate a function with an Optional return, then narrow an optional list before using it.

Rung 1 — annotate a function and its Optional return

Loading exercise…

Rung 2 — narrow an Optional in a list

Loading exercise…

Basic annotations are a habit; the next module adds the machinery that types the hard parts — generics, Protocols, and typed decorators.