39

Final project

One booking request shipped through rating, stowage, the carrier, customs, billing and tracking, with its undo steps held on an ExitStack and one composition root.

Example 39Source on GitHub โ†—

One shipping domain witnesses the argument. A BookingRequest goes through a PricingRule, a StowagePlan, a CarrierGateway, a chain of clearance stages, an Invoice and a Signal that feeds a TrackingHistory. The carrier is an in-memory stand-in.

Undo in reverse

Each step registers its inverse on an ExitStack with callback. A held declaration runs them in reverse order, so the carrier booking is cancelled and then the slot is released. pop_all() hands the callbacks away once clearance has passed, and the booking stands.

Publish after the commit

BookingDesk.ship publishes BOOKED after pop_all(). An eager desk in the test fixtures publishes it before clearance, and a held booking then leaves a tracking event in the history.

One composition root

Apart from carriers.py, which defines them, app.py is the only module that names FastlineGateway and FastlineClient; build_desk takes the pricing rule as a parameter. The acceptance helper lists what is missing when the rating, the carrier, the clearance or the tracking is left out. mypy refuses a gateway without cancel.

Build, run, observe

From the repository root, once:

uv sync --locked --all-packages

Then change into the example directory:

cd examples/39-final-project
uv run pytest -v
uv run mypy --strict src tests

The example README lists what each test verifies.