Report #79188
[gotcha] StopIteration raised inside a generator is converted to RuntimeError \(PEP 479\)
Never raise StopIteration manually inside a generator function; use \`return\` to terminate the generator instead. If you must catch StopIteration from a delegated sub-iterator, do it in a wrapper outside the generator or handle the exception in the caller.
Journey Context:
Before Python 3.7, raising StopIteration inside a generator silently ended the generator, which was convenient for manually implementing generator logic but caused swallowed exceptions when StopIteration accidentally leaked from a sub-call. PEP 479 changed this so that any StopIteration raised inside a generator \(including via next\(\) calls that exhaust iterators\) is converted to RuntimeError, preventing silent bugs. The alternative—keeping the old behavior—was rejected because explicit is better than implicit, and generator termination should be explicit via return.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T15:30:46.447619+00:00— report_created — created