liveBook · Manning

Type hints

As explained in Python How-To

Type hinting is the practice of annotating Python variables, parameters, or return values with their intended types without generally enforcing those types at runtime.

How can someone tell what to pass to a function before calling it? A function named `generate_stats(measures)` does not reveal that `measures` should be a list or tuple. Type hints make intended argument and return types visible in the function signature, helping callers use the function correctly. section 6.3

Annotate a function signature

```python from statistics import mean, stdev def generate_stats(measures: list) -> tuple: return mean(measures), stdev(measures) ``` Use `parameter: type` for an argument and `-> type` for the return value. Calling `help(generate_stats)` displays the annotated signature: `generate_stats(measures: list) -> tuple`. The hints describe the intended inputs and output; they do not enforce those types at runtime. section 6.3.2 section 6.3.1

Start with the simplest useful hint, then add detail where it helps a caller:

  • Annotate a variable with a colon, as in `number: int = 3`. Python remains dynamically typed: a later assignment can give that variable a value of another type. section 6.3.1
  • Put a parameter's type before its default value: `multiplier: int = 1`. A custom class can also serve as a parameter type. section 6.3.3
  • Specify container contents when `list` alone is too broad: `list[Task]` distinguishes task objects from strings. Use forms such as `dict[int, str]` or `tuple[float, ...]` when their contents matter. section 6.3.3
  • Use `|` for alternatives, such as `list[float] | tuple[float, ...]`. `Sequence[float]` from `typing` can express the shared sequence requirement instead. section 6.3.3 section 6.3.4
Table 6.1 Type hints for common built-in container objects (view table figure)

Container type

Code examples

Explanation

list

list[str]

A list of str objects

list[int]

A list of int objects

tuple

tuple[float, int]

A tuple of a float object and an int object

tuple[float, ...]

A tuple of multiple float objects

dict

dict[int, str]

A dict of keys using int objects and values using str objects

dict[int, list[int]]

A dict of keys using int objects and values using list objects of int objects

set

set[int]

A set of int objects

set[str]

A set of str objects

Compare common container annotations when choosing how precisely to describe a function's inputs. section 6.3.3

Hints also help an IDE flag incompatible arguments while you code. They complement rather than replace documentation or runtime checks. One important distinction: a data class uses field annotations to identify fields and generate methods such as `__init__`; an unannotated attribute will not become a field through that mechanism. section 6.3.2 section 9.2.2

How Python How-To covers it

How deep it goes
Works through variable, function, container, union, and custom-class annotations; introduces `typing.Sequence` without surveying the full `typing` module.
Examples
Uses a statistics function, annotated task containers, a finance-package challenge, and annotated data-class fields.
What it assumes
Assumes familiarity with functions and common built-in types, while teaching variable-annotation syntax before function annotations.
What you can do after
Build it from scratch: annotate Python functions and containers so callers and IDEs can identify intended types.
How it connects
Connects type hints to function signatures, default arguments, IDE warnings, the `typing` module, and data classes.

Also explained in

Compare how 17 books explain type hints →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.