Python 3 allows us to provide extensive type hints for describing the contents of a collection. This has two benefits:
- It helps us visualize the data structure.
- It supports running mypy to confirm that the code uses the data structures properly.
The non-collection types (int, str, float, complex, and so on) all use the type name as their type hint. The built-in collections all have parallel type definitions in the typing module. It's common to see from typing import List, Tuple, Dict, Set to import these type names into a module.
Each of the type hints accepts parameters to further narrow the definition:
- The List[T] hint claims the object will be a list and all the items will be of type T. For example [1, 1, 2, 3, 5, 8] can be described as List[int].
- A Set[T] hint is similar to the List[T] hint. It claims all items in the set will be...