Support generator functions and yield from
Merged
RE
8d ago+3853+3853+0123456789012345678901234567890123456789-307-307-012345678901234567890123456789
Re-implementation of pydantic/monty#1020 on top of main.
- Generator functions,
yield,yield fromandsend(), including lambdas and methods StopIteration(value)and.value, with generator return values keeping their identity- PEP 479: a
StopIterationescaping a generator or coroutine becomesRuntimeError, caused by the original - Generators advance through
py_next, sofor,next(),list()/sum()/sorted()and theitertoolsadaptors consume them - Host calls suspend inside generator bodies and
yield fromdelegates, and snapshots carry running and paused generators - CPython's
SyntaxErrors foryieldoutside a function or inside a comprehension, and foryield frominsideasync def - Dump version 18, with the released
dump_v17.binkept as an older fixture docs/limitations/generators.md
š¤ Generated with Claude Code
Summary by cubic
Implements generator functions and expressions, yield, yield from, and send(), which previously raised NotImplementedError. Generators run as pushed frames: yield returns to the caller and return raises StopIteration(value), so for and next() drive them as usual. Generator expressions now produce lazy generator objects instead of materializing to lists.
- Eager native consumers (
list,sum,sorted,itertoolsadaptors,str/bytes.join,list/deque.extend,set/dict/Counter.update) drain a generator argument in a suspendableforloop, so a generator body that calls the host works under them;any()/all()stop at the deciding item. The drain hook resolves the receiver method through the type's table, sohashlib.sha256().update(gen)andos.path.join(gen)no longer consume the argument.*unpacking cannot suspend, soasyncio.gather(*(f(x) for x in xs))with a hostfraisesNotImplementedError. sendand__next__are attribute-table methods on thegeneratortype, so unboundgenerator.send(g, x)resolves and checks its receiver like CPython.StopIteration(value)keeps.valueby reference, and an escapingStopIterationbecomesRuntimeError(PEP 479) caused by the original.- The parser rejects
yieldoutside a function, inside a comprehension, andyield frominsideasync def, with CPython'sSyntaxErrormessages. - Dumps carry running and paused generators ā including a host call suspended inside a generator or
yield fromdelegate ā and resume them; the dump format moves to version 18. - Documents limitations:
throw()/close(), generator introspection attributes, and async generators.