Lab 07: Type Hints & Generics
Objective
Time
Prerequisites
Tools
Lab Instructions
Step 1: Core Type Hints
docker run --rm zchencow/innozverse-python:latest python3 -c "
from typing import Optional, Union, Any, Callable, Sequence, Mapping
from collections.abc import Iterator, Generator, AsyncIterator
# Basic annotations
def greet(name: str, times: int = 1) -> str:
return (f'Hello, {name}! ' * times).strip()
# Optional (= X | None)
def find_product(product_id: int, catalog: dict[int, str]) -> Optional[str]:
return catalog.get(product_id)
# Union (pre-3.10 style, or use X | Y in 3.10+)
def process(value: Union[int, str, list[int]]) -> str:
if isinstance(value, int): return f'int:{value}'
if isinstance(value, str): return f'str:{value}'
return f'list:{sum(value)}'
# Complex types
def batch_process(
items: list[dict[str, Any]],
transform: Callable[[dict[str, Any]], dict[str, Any]],
filters: list[Callable[[dict[str, Any]], bool]] | None = None,
) -> list[dict[str, Any]]:
result = items
if filters:
for f in filters: result = [x for x in result if f(x)]
return [transform(x) for x in result]
catalog = {1: 'Surface Pro', 2: 'Surface Pen', 3: 'Office 365'}
print(greet('Dr. Chen', 2))
print(find_product(1, catalog))
print(find_product(99, catalog))
print(process(42))
print(process('hello'))
print(process([1, 2, 3]))
products = [
{'id': 1, 'name': 'Surface Pro', 'price': 864.0, 'stock': 15},
{'id': 2, 'name': 'Surface Pen', 'price': 49.99, 'stock': 80},
{'id': 3, 'name': 'USB-C Hub', 'price': 29.99, 'stock': 0},
]
result = batch_process(
products,
lambda p: {**p, 'value': p['price'] * p['stock']},
filters=[lambda p: p['stock'] > 0]
)
for r in result:
print(f' {r[\"name\"]}: value=\${r[\"value\"]:.2f}')
"Step 2: TypeVar & Generics
Steps 3–8: TypedDict, Literal, Protocol, Final, NewType, Capstone
Summary
Type
Purpose
Example
Further Reading
Last updated
