The Generator That Vanished — Python Bug Hunt

Inspired by a bug class unique to Python that has shipped wrong reports at real companies: a generator can be consumed exactly once.

  • Language: Python
  • Layer: Backend
  • Difficulty: Medium
  • Concepts: Iterators, Python Gotchas
  • Modelled on: Python generators
  • Visible tests: a generator is summarised correctly; plain lists still work
  • Reward: 50 XP for a complete fix

Briefing

Inspired by a bug class unique to Python that has shipped wrong reports at real companies: a generator can be consumed exactly once. Count its rows, then sum them — and the sum sees an empty sequence. Totals: zero. Nobody notices until finance does.

The row source (locked) returns a generator. report.py iterates it twice.

Bug report

BUG-GENEXH · Priority: High · Reported by: analytics QA

summarize(rows) -> {"count": n, "total": sum of amt}:

  • rows may be ANY iterable, including a one-shot generator
  • both numbers must reflect the same data

Observed: count is right, total is 0 — the second pass over the generator finds it already exhausted.

Logs

[report] rows=3 revenue_total=0

The code as shipped

src/data/report.py (editable)

# Summarises a row stream.

def summarize(rows):
    count = sum(1 for _ in rows)
    total = sum(r["amt"] for r in rows)
    return {"count": count, "total": total}

Read-only context: src/data/source.py.

Open the hunt to edit the files, run the visible tests and submit against the hidden ones. More Python bug hunts.