Python type hints and how to use them
function annotations, variable annotations, typing module, Optional, Union, List, Dict, Callable, dataclasses, mypy basics
Type Hints
Type hints are annotations that describe expected types. Python does not enforce them at runtime โ they are for static checkers (mypy, pyright) and IDE autocomplete.
def greet(name: str, times: int = 1) -> str:
return ("Hello, " + name + "! ") * times
from typing import Optional, List, Dict
def find_user(user_id: int) -> Optional[str]:
# returns str or None
users = {1: "Alice", 2: "Bob"}
return users.get(user_id)
def process(items: List[int]) -> Dict[str, int]:
return {"sum": sum(items), "count": len(items)}
Dataclasses
from dataclasses import dataclass, field
@dataclass
class Point:
x: float
y: float
label: str = "point"
tags: list = field(default_factory=list)
p = Point(1.0, 2.0)
print(p) # Point(x=1.0, y=2.0, label='point', tags=[])
print(p.x) # 1.0
@dataclass auto-generates __init__, __repr__, and __eq__. Use field(default_factory=list) for mutable defaults โ the same mutable default pitfall from regular functions applies here.
Type hints improve code quality in three concrete ways: IDEs provide better autocomplete and flag type mismatches in real time; static checkers like mypy catch bugs before tests run; and the annotations serve as machine-checked documentation that never goes stale. Adopt type hints incrementally โ start with function signatures, then add them to complex variables. Python 3.10+ simplifies union types with X | Y syntax instead of Union[X, Y]. The dataclass decorator is one of the most useful tools in modern Python: it eliminates boilerplate __init__ code and makes data-carrying classes concise and readable.
