pythonmypy1# hints.py2def double(n: int) -> int:3 return n * 24print(double(21))5print(double("ha"))print(double(int("21")))error: line 5Successn: int2142"ha"'haha'hints.pyline 5
Code card: hints.py; the highlighted line is the one being explainedpython runs the code; mypy only reads it and checks the typesTerminal: what print() and mypy writedouble.__annotations__: where Python keeps the hintsA type hint says what kind of value belongs where: n: int, -> int. Python files the hints in double.__annotations__ but never checks them while it runs, so double("ha") quietly returns haha. A type checker, mypy, reads the same file without running it, flags line 5, and reports Success once line 5 passes an int.

Writing hints

  1. A type hint is a note in the code that says what kind of value belongs where. On line 2, n: int says n should be an int (a whole number), and -> int says double gives back an int.

Run time

Check time

Try it yourself

;; step 1/9 · line 2: def double(n: int) -> int:
def double(n: int) -> int:   # n should be an int; returns an int
    return n * 2

# variables can carry a hint too:
count: int = 0
names: list[str] = []
maybe: int | None = None    # an int, or None

The typing documentation says it plainly: the Python runtime does not enforce function and variable type annotations. Tools such as type checkers, IDEs and linters use them. mypy prints each message on one long line; the terminal in the figure wraps it, and an indented row continues the row above.