Type hinting in Python lets you specify the expected data types of function arguments, return values, and variables. It does not change how Python runs your code, but it improves readability and allows tools (like IDEs and static type checkers) to catch potential bugs before runtime.
Type hints are optional, but they are very helpful in larger projects, team environments, and when maintaining code over time.
mypy, not by the Python interpreter.Basic function type hint syntax:
parameter_name: Type for annotating parameters.-> ReturnType for annotating the return value.Variable annotations use the form:
variable_name: Type = valueFor more complex types, Python provides the typing module, which includes Optional, Union, List, Dict, and many others. In modern Python, you can also use built-in generics like list[int] and dict[str, int].
# Define a function with typed parameters and return type
def add(a: int, b: int) -> int:
return a + b
# Annotate the variable that stores the result
result: int = add(5, 3)
# Declare variables with explicit type hints
name: str = "Alice"
age: int = 25
scores: list[int] = [90, 85, 88]
# Import typing helpers for Optional and Union
from typing import Optional, Union
# Greet a user whose name may be missing
def greet(name: Optional[str] = None) -> None:
if name:
print(f"Hello, {name}")
else:
print("Hello, Guest")
# Accept either int or float and process it
def process(value: Union[int, float]) -> None:
print(value * 2)
# Use type hints with lists and dictionaries
from typing import List, Dict
# A list of integers
numbers: List[int] = [1, 2, 3, 4]
# A dictionary mapping strings to strings
user_info: Dict[str, str] = {"name": "Alice", "city": "Mumbai"}
add(a: int, b: int) -> int, the hints say that both a and b should be integers and the function returns an integer. Calling add(5, 3) produces 8, and result is annotated as an int.name: str and scores: list[int] tell readers and tools what kind of values they should contain.greet(name: Optional[str] = None):
greet("Alice") prints Hello, Alice.greet() or greet(None) prints Hello, Guest.process(value: Union[int, float]), value can be either int or float. For example:
process(10) prints 20.process(2.5) prints 5.0.numbers holds integers and user_info maps strings to strings.mypy, pyright, or IDE inspections to check hints.dict[str, int]) over plain dict when possible.rectangle_area that takes width: float and height: float and returns a float. Add proper type hints.None using Optional. For example, a function that optionally takes a username and prints a welcome message.Dict[str, int] to store subject names and marks. Iterate and print them.mypy (or another type checker) on a small script and observe the warnings or errors it finds.