Python

8 Levels of Using Type Hints in Python

To make your programs more readable and easier to debug

Yang Zhou
TechToFreedom
Published in
6 min readDec 7, 2022

--

8 Levels of Using Type Hints in Python
Photo by Alice Donovan Rouse on Unsplash

Dynamic typing, which means that the type of a variable is determined only during runtime, makes Python flexible and convenient.

However, every coin has two sides. Messy typing of variables is usually the root of hidden bugs. Robust programs should be type-safe.

Therefore, Python provides the typing hint feature since version 3.5.

This feature is a good trade-off. It merely provides hints instead of changing Python into a static typing language. But it can make your Python programs:

  • More readable for other developers
  • Type safer to avoid hard-to-find bugs
  • Provide more information for modern IDEs’ automatic code completion
  • IDEs’ can help you check the potential bugs with the help of the type hints

Let’s see why it can make your programming life easier.

For example, the following function has an argument called data. You know it should be a string. But when you need to use some built-in methods for a string, the IDE (PyCharm in this case) can’t give you any clue.

--

--