An input contract the report enforces
Make the report refuse a file that breaks the rules, and confirm that refusing it leaves last month's report exactly as it was.
2 parts · 11 and 2 min · part 1 on a computer
Free course. Your Maestro, an AI, .
On the last page, your test functions in tests.py caught every flaw in November's Glossary word file. Those tests work when someone remembers to run tests.py, but nothing stops a colleague or a scheduled job from running report.py directly on sales_2026-11.csv.
An input contract puts those same rules inside the report Glossary word itself, right at the door. Look at run() in report.py: right after reading data/sales_{month}.csv, it calls problems = check(rows, columns, month) from contract.py. When problems comes back with even one message, report.run records "status": "rejected" in reports/runs.csv and returns immediately. It never reaches the loop that adds up amount, and it never writes to reports/history.csv or reports/latest.json.
Right now, the starter check() in contract.py only enforces two file-level rules: the header must match EXPECTED_COLUMNS (["order_id", "order_date", "region", "amount"]), and rows cannot be empty. Because sales_2026-11.csv has those four column names and six rows, check() returns [] and waves November through.
What happens next shows why a Python crash is no substitute for a contract. When you run the starter project, report.run("2026-11") loops through the rows and calls float(r["amount"]). Before anything stops it, the loop adds the negative order on line 3 (-80), records 300 under a blank region ("") on line 4, counts the duplicate S-1101 on line 5 a second time, and mixes in the October order (2026-10-31) on line 6. The run only blows up with a ValueError on line 7 because "1,200" contains a comma. If that last amount had been written as 1200, the script would have finished without a complaint and overwritten October's report with corrupted November totals.
Common mistake: Refuse bad input at the door instead of skipping or repairing rows
It is tempting to skip broken rows inside the loop or write code that guesses what the file meant, such as stripping commas or turning a negative amount positive. Either shortcut lets the report publish an "ok" total that silently drops real orders or invents figures finance never closed. A contract never guesses: it rejects the entire file, keeps the last valid report in place, and lists every problem so the upstream export can be fixed.
Try it
Start by running gate.py as it is (uv run gate.py) so you can see September and October pass (ok) and see where November crashes.
Then complete check(rows, columns, month) in contract.py so every row in the file is checked against four rules:
- Unique key:
row["order_id"]has not appeared earlier in the same file. - Date in month:
row["order_date"]starts withmonth + "-". - Allowed region:
row["region"]belongs toREGIONS. - Non-negative numeric amount:
row["amount"]converts cleanly withfloat(), and the resulting number is not less than0. Iffloat()raises aValueError, append a problem message andcontinueto the next row before comparing the amount to0.
Append one message to problems for each violation rather than returning early, so a single run reports all five problems in November at once. If you are unsure why check() collects a list of problems instead of stopping at the first bad row, ask your Maestro.
A good place to stop. Up next: part 2 of 2 · 2 min
When your contract is complete, uv run gate.py finishes cleanly without a traceback. You should see 2026-09 ok, 2026-10 ok, and 2026-11 rejected with five problem lines underneath it: the negative amount on line 3, the blank region on line 4, the duplicate S-1101 on line 5, the 2026-10-31 date on line 6, and the non-numeric '1,200' on line 7.
Check the last line of the terminal output as well: latest.json still reads {"month": "2026-10", "rows": 8, "total": 2800.0}. Even though gate.py ran November last, refusing the broken file at the door left October's published report untouched. If your check crashes on the comma in November's amount or misses one of the five problems, ask your Maestro to look at your loop.