- Phase: 8. Data, Web & APIs
- Duration: 2 hours
- Write function signatures with type hints
- Use the typing module (List, Dict, Tuple, Set, Optional, Union, Any, Callable)
- Define generic functions with TypeVar and Generic
- Run mypy for static type checking
- Understand gradual typing benefits
- Type hint syntax for functions and variables
- typing module types
- Optional and Union types
- Callable type for functions
- TypeVar and Generic for generics
- Running mypy
- Gradual typing approach
Modules 000-072.
from typing import List, Dict, Optional, Union, Callable, TypeVar, Generic
def process_items(items: List[str]) -> Dict[str, int]:
return {item: len(item) for item in items}
def greet(name: Optional[str] = None) -> str:
if name is None:
return "Hello, World!"
return f"Hello, {name}!"Note: From this module forward, ALL code examples include type hints.
- Python docs: typing module
- mypy documentation
- PEP 484 – Type Hints