What are Python Type Hints?
Type Hints in Python specify the type of variables or functions parameters as well as the return type. You can read more about them in the PEP 484 Guidelines.
In general they work like type declarations in every typed programming language, like Java or C++. The only difference is that Type Hints are completely optional. Python remains a dynamic language, because the Interpreter simply ignores the Type Hints. They are only relevant for development and have no influence on the execution. Accordingly, you can run a Python program with completely incorrect Type Hints without getting any errors.
Type Hints exist just for you as a developer to make your life easier – so you should use them!
Types in Python
Python has many built-in types, way more than you might have expected. Some common examples for build-in types are str for strings, int for integers and bool for boolean values.
Furthermore, types are objects like everything else in Python. By the way, you can actually use types as keys for a dictionary. So the following code really works!
Okay, you can do lots of things with the build-in types, but they are not really specific. For instance, you can have a variable of the type list, but what’s actually in the list? Nobody knows.
That’s why Python 3.5 introduced the typing module! You can import various types from this module, which you can specify really precisely. The following examples show some features of the typing module, but feel free to take some time and explore the module on your own.
How do Type Hints look like?
The syntax for Type Hints is, like the Python syntax in general, very simple.
In the following code example, you can see some type specifications for variables. The according syntax is just a colon after the variable name followed by the type.
For these examples the types might seem a bit redundant and obvious. In fact, you will use this kind of Type Hint rather seldom in comparison to the others. But when you use the returned object of a function for instance, you may not know the type of it.
More frequently used are Type Hints for parameters, and they basically work just the same as for variables. But note that this applies for parameters with default arguments too!
Another possibility to specify Type Hints is for the return type of a function. There is also another syntax for this case: You specify the return type with an “arrow”, followed by the type. It is important that the type is in the function definition before the colon.
This is the complete syntax you need to know if you want to use Type Hints! Easy, isn’t it?
Benefits of specifying types
Some IDEs like PyCharm support Type Hints in different ways. First of all, PyCharm complains when a Type Hint does not matches with your coding. Additionally, you get a suggestion what the type should be or what is expected. This prevents unintentional behaviours where you expect a type and are surprised when you get something completely different. With Type Hints, you don’t have to reengineer every function you want to call just for knowing what it will return anymore. In most cases, the combination of the function name and the type specifications make it clear how to use the function. If it doesn’t, you should improve the readability of your code, for example by applying the KISS Principle or giving Meaningful Names.
Another really useful feature is the auto-completion! A common use-case for this scenario is an instance object from a class with some attributes and methods.
If you call a function which returns an object, you might store it in a variable and work with it. Without Type Hints, the IDE wouldn’t know what alice is. If you type a point after the name, you won’t get any useful suggestions for functions or attributes of the Dog class. This changes when you add Type Hints!
Now you will get all possible suggestions for working with the class instance.
As we have seen, Type Hints are a really powerful tool to increase your productivity and motivation. It also greatly improves the readability of your code as everyone can quickly see what kind of objects you are working with. So all in all, you should definitely use Type Hints!
There is nothing that excites me more than solving problems and understanding complex concepts. I want to share this enthusiasm by making complex things as simple as possible, so everyone can understand them. Nothing is more frustrating than trying to understand a topic with incomprehensible explanations. That’s why I want to inspire you to become a better developer by simplifying concepts and explaining issues clearly.
Our Book Recommendations on Python (Affiliate)