What are type hints in Python?
Optional annotations that describe expected data types to improve readability, debugging, and tooling (not enforced at runtime).
Common basic type hints?
int, float, str, bool
What is Optional[T]?
Value can be type T or None
π Optional[T] = Union[T, None]
What is Union?
Allows multiple possible types
π Union[int, str]
Difference between List, Dict, Tuple?
List[T] β sequence of same type
Dict[K, V] β key-value pairs
Tuple[T1, T2] β fixed structure and length
Why are type hints useful?
Better readability, fewer bugs, IDE support, scalable code.
What is TypeVar?
A way to write generic functions that preserve input type in output.
What does TypeVar guarantee?
Input type = Output type (type consistency without restriction).
What is a Protocol?
Defines required behavior (methods), not exact type.
How does Protocol work?
Any object with required methods is accepted (no inheritance needed).
Where are type hints used in real software?
APIs (FastAPI), data pipelines, large backend systems, interfaces (Protocol).
Ultra-Short Revision
Type hints β documentation + safety
Optional β maybe None
Union β multiple types
List/Dict/Tuple β structured data
TypeVar β same type in/out
Protocol β behavior-based typing