Part 02 / Topic 4 of 6
Types and dataclasses
Integration code is mostly data in motion, and bugs hide in implicit shapes ("is weight a float or the string '120 kg'?"). Make shapes explicit:
from dataclasses import dataclass
@dataclass
class Shipment:
shipment_id: str
weight_kg: float | None # None: weight missing in the source
status: str
shipped_on: str # ISO date; parsed properly in Part 6
A dataclass gives you a constructor, equality, and a readable repr; the type hints give the reader (and your editor) the shape at a glance. In Part 6 you will upgrade this idea with Pydantic, which enforces the shapes at runtime against real messy data. Dataclasses say what the shape is; Pydantic also refuses data that does not match.
Done here? The practice set for Part 2 — 4 exercises with solutions — is on the part page.