Article by Ayman Alheraki on January 11 2026 10:36 AM
Type hints in Python are a powerful feature introduced in Python 3.5 to improve code clarity, enhance maintainability, and reduce errors during development. However, since Python remains a dynamically typed language, type hints do not affect program execution but serve as documentation to guide developers and tools like mypy.
Type hints are a way to specify expected data types for variables, function parameters, and return values. This is done using annotations that indicate the expected data type without enforcing strict type checking as in languages like C++ or Java.
Improves code readability – Makes it easier to understand what data types are expected.
Reduces programming errors – Helps detect errors before execution when using tools like mypy.
Enhances the developer experience – Provides better integration with IDEs like VS Code and PyCharm, enabling autocomplete suggestions.
Improves documentation – Makes the code self-documenting without requiring additional comments.
You can use type hints with variables to specify the expected data type:
# Without Type Hints (unclear)name = "Ali"age = 30height = 1.75
# With Type Hints (clearer)name: str = "Ali"age: int = 30height: float = 1.75Type hints do not affect program execution; they only serve as documentation to improve code correctness.
Type hints can define parameter types and return types for functions.
def add(x, y): return x + yHere, there is no indication of what data types x and y should be, which may lead to unexpected errors.
def add(x: int, y: int) -> int: return x + yHere, we specify that x and y must be integers (int) and that the function will return an integer (int).
def greet(name: str) -> str: return f"Hello, {name}!"
Python supports complex data types, such as lists, dictionaries, and sets, using the typing module.
from typing import List
def sum_numbers(numbers: List[int]) -> int: return sum(numbers)
result = sum_numbers([1, 2, 3])Here, List[int] means that numbers must be a list containing only integers.
Sometimes, a function should accept multiple data types. This can be achieved using Union:
from typing import Union
def process_value(value: Union[int, float]) -> float: return value * 2.5Here, value can be either an int or a float, making the function more flexible.
When a function parameter can be None in addition to a specific type, use Optional:
from typing import Optional
def get_username(user_id: int) -> Optional[str]: if user_id == 1: return "Admin" return NoneThis means the function may return a string (str) or None, making the result optional.
If you want a function to accept any data type without restrictions, use Any:
from typing import Any
def print_value(value: Any) -> None: print(f"Value: {value}")Here, value can be a string, a number, a list, or any other type.
You can specify the key and value types when working with dictionaries:
from typing import Dict
def get_student_scores() -> Dict[str, int]: return {"Ali": 95, "Sara": 88, "Omar": 76}Here, Dict[str, int] means that keys must be strings (str), and values must be integers (int).
You can use type hints when defining classes:
class Person: def __init__(self, name: str, age: int): self.name: str = name self.age: int = age
def get_info(self) -> str: return f"{self.name} is {self.age} years old."
person = Person("Ahmed", 28)print(person.get_info())Here, we specify the type of name and age, making the code more readable and maintainable.
You can use mypy to check for type hint errors before running the code.
mypy:pip install mypymypy on a Python Script:mypy script.pymypy will help detect any type mismatches in the code.
Type hints in Python are a powerful feature that improves code clarity, error detection, and maintainability. While they remain optional, they help create more professional and scalable programs. Thus, using type hints is highly recommended, especially for large projects or collaborative development teams.
By adopting Type Hints, you can write more structured, reliable, and professional code.