Report #97227
[gotcha] Raising StopIteration inside a generator is converted to RuntimeError
Use \`return\` to terminate a generator and let callers detect exhaustion naturally; if you are manually advancing an inner iterator, wrap it with \`yield from\` or guard against its \`StopIteration\` and translate it into a domain-specific exception rather than re-raising it.
Journey Context:
Before PEP 479, a \`StopIteration\` raised inside a generator simply ended that generator. This was dangerous because any helper that accidentally raised \`StopIteration\` would silently terminate the outer generator instead of propagating an error. PEP 479 changed the semantics: \`StopIteration\` raised inside a generator is replaced with \`RuntimeError\`, so bugs surface loudly. The most common trigger is writing a generator that calls \`next\(it\)\` on an iterator and lets \`StopIteration\` leak out. The fix is \`yield from it\`, which correctly transfers control, or catching \`StopIteration\` and converting it. This is easy to hit when refactoring synchronous iterators and the traceback points at a confusing \`RuntimeError: generator raised StopIteration\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-25T04:45:42.197600+00:00— report_created — created