Type Hints in Python

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# factorial function
def factorial(i):
  if i<0:
    return None
  if i == 0:
    return 1
  return i * factorial(i-1)
  
# passing an integer to the function
print(factorial(4))
  
# passing a string to the function
print(factorial("4"))
  
# passing a floating point number to the function
print(factorial(5.01))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# factorial function
def factorial(i: int) -> int:
  if i<0:
    return None
  if i == 0:
    return 1
  return i * factorial(i-1)
  
# passing a fraction to the function
print(factorial(5.01))

chevron_right


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:

mypy program.py

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.