Python Type hints came is available in Python version 3.5 and later. Type hinting does not mean that you are typing some syntax on the IDE you are using and the tool is giving you options related to the syntax. Type hinting is the formal solution to indicate what type of the value your python code is expected. Type hinting does not create any error in the code or deviates the performance of the code. It helps you in understanding what kind of input code is going to expect.
Python is a dynamic typed language and users do not need to pass the data type for the variable like other languages. For instance, to define an integer variable we can write variable1 = 5, python auto-assign the datatype for variable1 as an integer
Is type hinting is useful in dynamically typed language?
variable1 = 5
type(variable1)
<class ‘int’>
But in some cases, while writing the complex code dynamic typing can lead to having some bugs, which becomes very difficult to find and resolve. Type hinting is going to help what you are expected and where you are doing a mistake. Type hinting will not affect your code, it won’t throw an error, type hinting will only give an additional check to the code.
Why to use type hints
- Help in debugging
When you have written a piece of code and executing the code, sometimes you provide the wrong data types, Python will throw an error. But if you have used the type hinting Python is going to tell you what is the input type your code is expecting and what will be output type
For instance, you need to add two integers. While passing the input you passed the value as 10 and ‘20’, where input1 is an integer type, and the other is a string. Your code is going to throw an error. But if you have used the type hinting you will be aware what input should be passed and the IDE will raise an alert if you pass the wrong type for the input variable
- Helps with documentation
Assume code is written by one person and another person is reviewing the code. He/she need to go each and every line of the code to understand what could be expected result type. Type hinting helps them to understand the code in an easy way and avoid Type errors
Consider below two functions, funct1 without type hinting and funct2 with the type hinting. In funct1 to get an idea of what the function is going to return, the user needs to look into the return statement. Here we are passing input and adding 0.5 to the input, so the input can be an integer or float. Adding 0.5 makes an assumption datatype should be float else it will neglect the fraction value. Funct2 is giving a straight idea by looking at the function definition, we are passing the input with datatype integer and we are expecting output as a float. Type hinting makes the debugging of the code easier.
def funct1(input):
return input + 0.5
def funct2(input: int) -> float :
return input + 0.5
python mypy library can be useful while working with the type hinting. mypy checks the error if any in the file code where type hinting is used and throws the error by highlighting it. To begin with, the type hinting mypy is a good option
pip install mypy
mypy <file_name>.py
def funct2(input: int) -> float :
return input + 0.5
funct2(‘10’)
In the above example string is passed as input to the function. Python will throw an error as string data cannot be added to the integer.
Run Method1: Run this code as python file_name.py
Traceback (most recent call last):
File “file_name.py”, line 8, in <module>
print(funct1(’10’))
File “file_name.py”, line 2, in funct1
return input + 0.5
TypeError: can only concatenate str (not “float”) to str
Run Method2: Run the same code using mypy
Mypy file_name.py
File_name.py:2: error: Argument 1 to “funct2” has incompatible type “str”; expected “int”
Found 1 error in 1 file (checked 1 source file)
Both the ways error message is the same, using mypy is smaller and simpler to read and understand.Kindly share your feedback and questions and subscribe Hadoop Tech and our facebook page(Hadoop Tech) for more articles. Follow on GitHub
