Python is a dynamically typed language, which means you never have to explicitly indicate what kind of types variable has. But in some cases, dynamic typing can lead to some bugs that are very difficult to debug and in those cases, Type Hints or Static Typing can be convenient. Type Hints has been introduced as a new feature in Python 3.5.
Let’s look at this factorial function to understand this clearly:
If we try to pass an integer to the function it works correctly. Now, if we try to pass a string to the factorial function, we will get an error, as we can’t do a mathematical comparison with string.
Also, if we try to pass a floating point number, let’s say 5.01 we will also get an error here, as we are decrementing the number by 1 according to our logic in each iteration and it becomes -0.99 in the last iteration which returns “None”, which leads to an error as we are trying to multiply “None” with other floating point numbers.
The Type-Hints code looks as follows:
We have to specify the type of argument & also the type of the factorial function.
The Type-Hint is completely ignored by the Python interpreter. So, if we run this code again, we still get the same error.
So, we have to use a static type checker that analyzes our code and tries to detect if we are violating our Type-Hints or not.
The best known type checker is “mypy“. We can install it by simply using pip .
pip install mypy
To run the code now, we have to simply call the Python interpreter and we have to specify “-m” to indicate that we want to load a module and then specify the script which we want to check. For example:
Now, if we run this it will not actually execute our “
program.py” script but it will analyze it and will give us an error as like “Argument 1 to factorial has incompatible type float; expected int ” and which is a very clear message and that makes us much easier to debug our code as previously without mypy type checker the error message was not that much specific. And if we run the code again with an integer, it will work as fine.
- Python | type() function
- Type Conversion in Python
- type and isinstance in Python
- Python | Data Type | Question 1
- Python | Data Type | Question 2
- Python | Float type and its methods
- Python - Split heterogeneous type list
- Python | Type casting whole List and Matrix
- Python | Type conversion of dictionary items
- Python - Pearson type-3 Distribution in Statistics
- Determine the type of an image in Python using imghdr
- Python | Type conversion in dictionary values
- Data type Object (dtype) in NumPy Python
- Python | Convert heterogenous type String to List
- Python | Working with the Image Data Type in pillow
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.