Analyze code to suggest and add Design by Contract specifications (preconditions, postconditions, invariants, semantic invariants) in any language. Trigger on: "Design by Contract", "DBC", "preconditions", "postconditions", "class invariants", "code contracts", "formal specifications", "document assumptions", "add contracts", "make this function safer", "define what this function guarantees", "add assertions to document behavior", Bertrand Meyer, Eiffel contracts, or when discussing invariants that should always hold. Do NOT skip for non-Eiffel code — DBC applies everywhere via assertions, type guards, properties.
92
90%
Does it follow best practices?
Impact
95%
1.05xAverage score across 5 eval scenarios
Passed
No known issues
A commerce team has a discount helper that is reused by checkout, invoices, and reports. Recent incidents came from negative totals, unknown customer tiers, and callers assuming the item list is never changed. Review the function and add appropriate behavioral specifications and runtime checks while preserving the intended discount behavior.
Produce contract_analysis.md, discounts.py, and contract_tests.py with a few focused tests or examples for both normal and invalid inputs.
=============== FILE: discounts.py ===============
def apply_discount(order_total, customer_tier, items):
if customer_tier == "gold":
discount = 0.2
elif customer_tier == "silver":
discount = 0.1
else:
discount = 0.0
return order_total * (1 - discount)