What is Type Hints in Python

Type hints is a feature of Python that allows you to explicitly declare the data type of a variable when declaring it. They are only available in Python 3.5 and later.

Type hints provide two benefits. First, they help people reading your code to know what types of data to expect. Second, they can be used by the Python interpreter to check your code for errors at runtime, saving you from some frustrating bugs.

In the low-level programming languages like Java, C, C++, and C#, variables are strictly typed - which means you have to indicate the data type when creating a variable.

For example, creating variables in Java:

String name = "John";       # 'name' can only store a string

int age;                    # only an integer can be assigned to age

age = 23;                   # assigning a value to age


Python is considered a dynamically typed language, meaning that the types of variables need not be declared before use. This means that it is easier to code, but also means it is harder to catch errors in complex programs.
Example of  dynamic declaration of a variable in python.

name = "John"

age = 23

With this method of coding, Python automatically figures out the type of data that has been assigned to a variable. You can verify that by using the type() function:

print(type(name))              # <class 'str'>
print(type(age))               # <class 'int'>


Using type hints in your python code

In python, if you want to specify the data types of your variables, we use type hints as such:

variable_name: variable_type = value

weather: str = "cloudy"

degrees: int                    # you can declare a variable before assigning
degrees = 32


Also note that even after a variable type is indicated in Python, you can still assign a different data type to the variable:

degrees: int = 32
print(degrees)                      # 32

degrees = "thirty-two"
print(degrees)                      # thirty-two


Specifying a variable type in Python does not mean the variable can only accept values of that type. They are just hints that inform a user of the type which a variable is.


Adding types to functions

Type hints in Python also allows you to explicitly declare the type of a function's parameters and return value. 

For example, here is a simple function that takes in two arguments and returns the sum.

def sum(a, b):
    return a + b

By looking at this code, one cannot be sure if the caller is going to supply integers as the argument or strings or any other data type.

A call to this function works when supplied with int values and with other values such as strings, lists, and tuples:

sum(3, 5)                               # 8

sum([1, 0, 5], ['java', 'c++'])         # [1, 0, 5, 'java', 'c++']

sum('2', '3')                           # 23

Here you can see that the sum() function works well when it is invoked with either int values, string values, or even lists.

But the goal of the sum() function is to add two integers, and not two lists or strings. We can now provide type hints for the parameters in the Function definition indicating the type that we want to allow.

To indicate that we only want to allow int types we can change our function definition to look like:

def sum(a: int, b: int):
    return a + b

This informs the function caller that the arguments required for the sum() function should be integers not otherwise.

Similarly, to indicate only str types are allowed, we'd change our function to specify it as:

def sum(a: str, b: str):
    return a + b


Specifying A function's return type.

One could also indicate the data type of the value a function returns:

def sum(number1, number2) -> int :
    return number1 + number1

This informs the caller that the return type of the sum() function should be an integer.


Type hints are also sometimes called type annotations, type hints, or parameter type declarations.

Read More: 8 Excellent Python Courses on Udemy (2021)