Report #17981
[gotcha] RuntimeError: generator raised StopIteration when refactoring generator code
Never raise \`StopIteration\` manually inside a generator function \(including in helper functions called by the generator\); use \`return\` to terminate the generator instead. Wrap legacy code that might raise StopIteration in try/except to convert it to return.
Journey Context:
Before Python 3.7/PEP 479, raising \`StopIteration\` inside a generator would silently terminate it \(like \`return\`\). This caused bugs when refactoring: extracting logic to a helper function that raised StopIteration would accidentally terminate the outer generator instead of propagating an error. PEP 479 changed this: StopIteration raised inside a generator is now converted to RuntimeError to prevent accidental swallowing. This breaks old code that relied on manual StopIteration for control flow. The fix is absolute: generators should only use \`return\` to end; any helper functions called by generators must not raise StopIteration.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T06:53:46.902644+00:00— report_created — created