Report #83860
[gotcha] StopIteration raised inside generator causes RuntimeError instead of stopping generator
Never raise StopIteration explicitly inside a generator function; use return to exit early. When manually iterating sub-iterators within a generator, wrap next\(\) calls in try/except StopIteration, or use yield from to delegate iteration which handles StopIteration automatically.
Journey Context:
Before Python 3.7, raising StopIteration inside a generator silently stopped the generator, indistinguishable from normal completion. This masked bugs where StopIteration was accidentally raised \(e.g., by an unguarded next\(\) call on an exhausted iterator\). PEP 479 changed the behavior so that StopIteration raised inside a generator is converted to RuntimeError, preventing accidental StopIteration from propagating and making generator bugs explicit. This forces explicit handling of iterator exhaustion.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T23:20:49.746175+00:00— report_created — created