Code review example: 'Newest first' but sorted ascending: the order is reversed in Python
Retired from the DiffDojo practice pool in October 2026; the review below is the canonical answer. An AI wrote this pull request for the task underneath. Read the diff and decide what you would say before you scroll to the review.
The task
List orders needing attention
- Return orders that need attention: status is 'pending' OR 'failed'.
- Exclude orders that have been cancelled, even if they are pending or failed.
- Return them newest first.
Adds the attention-list endpoint: filters orders down to the ones that need attention and returns them newest first. Matches the sample data.
The diff
app/routes/attention.py+14The review
Issue 1: 'Newest first' but sorted ascending: the order is reversed
- The filter is correct; the order is backwards. The comprehension picks exactly the right orders,
pending/failed, not cancelled. Thensorted(orders, key=lambda o: o.created_at)returns them ascending: oldest first. - Requirement 3 said newest first.
sorteddefaults to ascending and there's noreverse=True, so the most recent orders that need attention land at the bottom of the list instead of the top. - This is the requirement-fidelity trap. Everything reads plausibly and the set of results is right; only the direction contradicts the spec, and only reading requirement 3 against the code catches it.
The fix: sorted(orders, key=lambda o: o.created_at, reverse=True).
The fix
return sorted(orders, key=lambda o: o.created_at, reverse=True)Why it matters
A sort next to a 'newest/highest/most recent first' requirement → check the direction against the words. sorted and .sort() are ascending by default; the spec is often descending.
Real-world stakes: an ops queue that's supposed to surface the freshest problems buries them under weeks-old ones, so the newest failed payments are the last thing anyone sees. Requirement-fidelity bugs like a flipped sort are exactly what assistants produce, the code is clean and correct in every way except the one the requirement actually pinned.
The verdict
Request changes. 'Newest first' but sorted ascending: the order is reversed
Try a live one
Python PRs you have not seen the answer to.
Leave inline comments and a verdict, then see what you caught. Free, no signup.
Review a Python PR →