What are type hints, and implement them using mypy in Python?

Type hints in Python programming, introduced in Python 3.5, specify the types of function parameters, class attributes, and variables. They are also used to represent the return type of a function, justifying the code’s clarity and maintainability.

Why do we need them?

There are various reasons why the type hint is an important concept in Python:

  • It helps to identify type-related bugs early in the development process, preventing runtime errors.

  • It makes our code more readable and self-explanatory by clearly describing the expected return type.

  • It helps to document the code by explicitly defining the functions’ parameter values and return types in the document. 

  • It enables using various Python tools and libraries for code analysis, refactoring, and generation.

  • It promotes cleaner code and encourages thoughtful consideration of types in our Python program.

Implementation

The most commonly used tool to check and implement type hints in Python code is mypyy. It is a static type checker for Python that analyzes the code and checks whether the types mentioned in the code match the actual usage. It identifies type-related errors, which makes it a valuable tool for improving code quality and reliability.

We can use the following command to install the mypy tool in our system:

pip3 install mypy

Example

Let’s have a look at the coding example of type hints:

def calculate_square(n: int) -> int:
return n * n
def main():
print(calculate_square("Five"))

Explanation

Let’s break the code written above:

  • Lines 1–2: We define a calculate_square() function, calculating the square of the passed integer argument.

  • Lines 4–5: We define the main() function, calling the calculate_square() function and passing a string value as an argument.

The output will result in the following errors:

main.py:4: error: Function is missing a return type annotation [no-untyped-def]
main.py:4: note: Use "-> None" if function does not return a value
main.py:5: error: Argument 1 to "calculate_square" has incompatible type "str"; expected "int" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
Resultant errors

Let’s look at the errors thrown in detail below:

  • The main() function has a missing return type, resulting in a type error.

  • In the calculate_average() function, a string was passed as a parameter. Whereas an integer value was expected.

The following code will execute successfully. We have incorporated the return type of the main() function and the correct type of argument.

def calculate_square(n: int) -> int:
return n * n
def main() -> None:
print(calculate_square(5))

Unlock the power of Python with Learn Python 3 from Scratch. Master the basics and gain the skills to build real-world projects, setting the foundation for a lifelong journey in computing.

Conclusion

Type hints, widespread in Python programming, simplify, read, and maintain code. The code is more reliable, and problems do not appear during runtime if searched within the type’s context. Type hints are useful while developing Python programs since they support the correctness of the types and other tools like mypy.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved