feat: add percentage discount helper for demo review#3
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Review Summary by QodoAdd percentage discount helper function
WalkthroughsDescription• Add percentage discount helper function for pricing calculations • Applies discount before tax calculation using multiplier approach • Intentionally minimal implementation to facilitate review discussion Diagramflowchart LR
subtotal["subtotal"] -- "multiply by 1 - discount_rate" --> discounted["discounted_subtotal"]
discounted -- "apply sales_tax_rate" --> total["final total"]
File Changes1. demo_app.py
|
Code Review by Qodo
|
| """Apply a percentage discount before tax.""" | ||
|
|
||
| discounted_subtotal = subtotal * (1 - discount_rate) | ||
| return calculate_total(discounted_subtotal, sales_tax_rate=sales_tax_rate) |
There was a problem hiding this comment.
1. Unbounded percentage discount math 🐞 Bug ≡ Correctness
calculate_total_with_percentage_discount computes subtotal * (1 - discount_rate) without validating discount_rate, so discount_rate > 1 yields a negative total and discount_rate < 0 increases the subtotal despite the function claiming to apply a discount.
Agent Prompt
### Issue description
`calculate_total_with_percentage_discount()` can return negative totals or unintended surcharges because it does not validate `discount_rate` before applying `subtotal * (1 - discount_rate)`.
### Issue Context
The existing absolute discount helper rejects negative discounts and clamps the discounted subtotal to `>= 0.0`, but the new percentage helper lacks equivalent protections.
### Fix Focus Areas
- demo_app.py[33-41]
- demo_app.py[23-30]
### Suggested fix
- Enforce a clear contract for `discount_rate` (e.g., raise `ValueError` unless `0.0 <= discount_rate <= 1.0`, or clamp into that range).
- Prevent negative subtotals similarly to the absolute discount helper (e.g., `discounted_subtotal = max(subtotal * (1 - discount_rate), 0.0)`), if the business rule is “never return a negative total.”
- Consider adding a brief docstring note describing allowed range and behavior when out of range (raise vs clamp).
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| ) -> float: | ||
| """Apply a percentage discount before tax.""" | ||
|
|
||
| discounted_subtotal = subtotal * (1 - discount_rate) |
There was a problem hiding this comment.
We should validate or clamp discount_rate here. Values outside the expected range can turn this helper into a surcharge path or produce a negative total, and that kind of pricing bug is easy to miss because the formula still looks mathematically valid.
| """Apply a percentage discount before tax.""" | ||
|
|
||
| discounted_subtotal = subtotal * (1 - discount_rate) | ||
| return calculate_total(discounted_subtotal, sales_tax_rate=sales_tax_rate) |
There was a problem hiding this comment.
I would rather route percentage discounts through the same normalization path as the absolute-discount helper instead of creating a second calculation flow. Keeping one shared path for validation, tax, and rounding makes future pricing changes less likely to drift.
| sales_tax_rate: float, | ||
| discount_rate: float, | ||
| ) -> float: | ||
| """Apply a percentage discount before tax.""" |
There was a problem hiding this comment.
Please make the business rule explicit in the docstring: is discount_rate expected to be 0..1, and does this percentage discount apply before tax the same way as the absolute-discount helper? Those semantics affect invoice behavior, so I would not leave them implied by the implementation.
Demo goal
Create a fresh, small PR that is easy to review and likely to produce high-signal review comments for the github-pr-kb Phase 8 demo.
What changed
Review prompts