Report #12044
[gotcha] StopIteration raised inside generator is converted to RuntimeError \(PEP 479\)
Never raise StopIteration manually inside generators or coroutines; use \`return\` \(or \`return value\`\) to terminate iteration. Wrap legacy code that raises StopIteration in a try/except that converts it to a return.
Journey Context:
Before PEP 479, raising StopIteration inside a generator simply stopped iteration, allowing silent data loss when refactoring loops into generators \(e.g., a helper that raises StopIteration when exhausted\). Python 3.7\+ converts this to RuntimeError to prevent bugs, breaking legacy code that relied on the old behavior. The fix is explicit: generators should always use \`return\` to exit; if refactoring old code, ensure StopIteration is never raised explicitly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:54:18.179638+00:00— report_created — created