Report #13650
[gotcha] Raising StopIteration inside generator stops iteration instead of propagating error
Never raise StopIteration manually in generators; use return or allow natural iteration to end
Journey Context:
Before PEP 479 \(Python 3.7\+\), raising StopIteration inside a generator would silently terminate the generator, making it indistinguishable from normal exhaustion. This masked bugs where StopIteration leaked from nested \`next\(\)\` calls or internal iterators. PEP 479 changed this so that StopIteration raised inside a generator is converted to RuntimeError. Developers still encounter this when manually implementing iterators: raising StopIteration is correct in \`\_\_next\_\_\` but deadly in generators \(functions using yield\). The correct pattern is to use \`return\` or simply fall off the end of the function.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T19:18:39.078345+00:00— report_created — created