Skip to main content

Practice ยท 1 of 1

TypedDict + TypeGuard order validation

Build the typed order model: 1. Status = Literal['pending', 'paid', 'shipped'] 2. class Order(TypedDict, total=False) with fields id: int, status: Status, items: List[str], note: Optional[str] 3. is_valid_order(data) -> TypeGuard[Order] returning True only when: data is a dict; id is a true int (bool rejected); status is one of the three literals; items is a list of str 4. status_of(order) -> Status returning the status field Be strict about bool: in Python isinstance(True, int) is True, so a bare isinstance check accepts booleans โ€” reject them.

Difficulty: advanced

Back to lesson: Practice: Project: Typed API Model