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.

Python · FastAPI · Requirement fidelity · intermediate

The task

List orders needing attention

  1. Return orders that need attention: status is 'pending' OR 'failed'.
  2. Exclude orders that have been cancelled, even if they are pending or failed.
  3. 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+14
1+ from fastapi import APIRouter
2+ from app.orders import all_orders
3+
4+ router = APIRouter()
5+
6+
7+ @router.get("/orders/attention")
8+ async def orders_needing_attention():
9+ orders = [
10+ o
11+ for o in all_orders()
12+ if o.status in ("pending", "failed") and not o.cancelled
13+ ]
14+ return sorted(orders, key=lambda o: o.created_at)

The review

Issue 1: 'Newest first' but sorted ascending: the order is reversed

app/routes/attention.py, line 14

  1. The filter is correct; the order is backwards. The comprehension picks exactly the right orders, pending/failed, not cancelled. Then sorted(orders, key=lambda o: o.created_at) returns them ascending: oldest first.
  2. Requirement 3 said newest first. sorted defaults to ascending and there's no reverse=True, so the most recent orders that need attention land at the bottom of the list instead of the top.
  3. 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.

Read more: python sorted reverse descending, sort newest first python, sorted key reverse true

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 →

How to review Python code · All code review examples