Report #76633
[gotcha] Manually raising StopIteration inside generator raises RuntimeError instead of terminating generator
Never explicitly \`raise StopIteration\` in generators or coroutines; use \`return\` to exit with a value \(available in generators since Python 3.3\). When delegating to sub-iterators, use \`yield from\` rather than manual \`next\(\)\` calls that might bubble up \`StopIteration\`.
Journey Context:
Before PEP 479 \(Python 3.7\+\), raising \`StopIteration\` inside a generator terminated it silently. However, this masked bugs where nested \`next\(\)\` calls on exhausted iterators would accidentally terminate the outer generator. PEP 679 changed this so that \`StopIteration\` raised inside a generator is converted to \`RuntimeError\`. The footgun is twofold: \(1\) legacy code or old tutorials explicitly using \`raise StopIteration\` now crashes, and \(2\) calling \`next\(\)\` on a sub-iterator inside a generator \(without try/except\) will raise \`StopIteration\` when exhausted, which now becomes \`RuntimeError\` instead of naturally terminating the generator. The correct pattern is \`yield from\` for delegation and \`return\` for generator return values.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T11:13:02.831391+00:00— report_created — created