Report #72503
[gotcha] StopIteration raised inside generator is converted to RuntimeError \(PEP 479\)
Never raise \`StopIteration\` explicitly in a generator; use \`return\` to terminate. When manually wrapping iterators with \`next\(\)\`, catch \`StopIteration\` and handle it locally \(e.g., \`break\` or \`return\`\), do not let it bubble up.
Journey Context:
Before Python 3.7 \(PEP 479\), raising \`StopIteration\` inside a generator simply terminated the generator silently. This was error-prone because accidentally exhausting a sub-iterator with \`next\(\)\` and letting \`StopIteration\` bubble up would silently truncate the generator instead of raising an error. PEP 479 changed this so that \`StopIteration\` raised inside a generator is converted to \`RuntimeError\`. The gotcha for modern Python is that manual iteration with \`next\(\)\` inside a generator now requires explicit \`try/except StopIteration\` handling; letting it propagate causes a crash. The fix is to use \`yield from\` for delegation \(which correctly handles the protocol\), or manually catch and terminate with \`return\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T04:17:08.756428+00:00— report_created — created