Appendix C. A brief introduction to type hints

 

Python introduced type hints (or type annotations) as an official part of the language through PEP 484 and Python version 3.5. Since then, type hints have become more common throughout many Python codebases, and the language has added more robust support for them. Type hints are used in every source code listing in this book. In this short appendix, I aim to introduce type hints, explain why they are useful, explain some of their problems, and link you to more in-depth resources about them.

Warning

This appendix is not meant to be comprehensive. Instead, it’s a brief kickstart. Please see the official Python documentation for details: https://docs.python.org/3/library/typing.html.

C.1 What are type hints?

Type hints are a way of annotating the expected types of variables, function parameters, and function return types in Python. In other words, they are a way that a programmer can indicate the type that is expected in a certain part of a Python program. Most Python programs are written without type hints. In fact, before reading this book, even if you are an intermediate Python programmer, it is very possible that you have never seen a Python program with type hints.

C.2 What do type hints look like?

C.3 Why are type hints useful?

C.4 What are the downsides of type hints?

C.5 Getting more information