Gradual typing & mypy
P31.typing.01 · Audience: Guest, IT/ML learner, Language professional
Where next?
Go up a level
P31.typing.01 · Audience: Guest, IT/ML learner, Language professional
Go up a level
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.
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 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.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Annotate a function with an Optional return, then narrow an
optional list before using it.
Loading exercise…
Loading exercise…
Basic annotations are a habit; the next module adds the machinery that types the hard parts — generics, Protocols, and typed decorators.
Loading your notes...