You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
types._GeneratorWrapper is no longer consistent with types.GeneratorType/types.CoroutineType #143552
types.coroutine() uses _GeneratorWrapper as a wrapper for generator iterators in cases where the callable object is not a user-defined function (but is, for example, a generator function compiled via Cython/Nuitka). However, the implementation has not been updated for 11 years and therefore does not provide certain properties, such as:
Interestingly, there was already an attempt to add *_suspended in #133373 (see #133372 (comment)), but the PR was closed in favor of removing the TODO comment, and this side issue was apparently overlooked.
I would add that the _GeneratorWrapper implementation itself is somewhat questionable. If we look at how generator-based coroutine objects behave, we see that they do not implement the types.CoroutineType interface (and in a sense, even the collections.abc.Coroutine interface), but only make generator iterators awaitable:
This difference in behavior for different types of passed callable objects may be unexpected (and is it even correct to say that generator-based coroutines are actually considered coroutine objects, as stated in the documentation?). There are three ways to deal with this:
Remove the cr_* properties.
Always use _GeneratorWrapper.
Make generator-based coroutines a separate type.
The third option has been rejected (see the PR mentioned above). The second option, ironically, may not be very good in terms of performance, especially when types.coroutine() is used to yield objects to the event loop.
Initially, _GeneratorWrapper was added in 13f7723 (see #68504; PEP 492). It did not have the cr_* properties, thus replicating only the types.GeneratorType interface (the only difference was having the __await__() method). These properties were added in 5376ba9 (see #68588; PEP 492). This was probably done by mistake.
Activity
x42005e1f commentedon Jan 8, 2026
I would add that the
_GeneratorWrapperimplementation itself is somewhat questionable. If we look at how generator-based coroutine objects behave, we see that they do not implement thetypes.CoroutineTypeinterface (and in a sense, even thecollections.abc.Coroutineinterface), but only make generator iterators awaitable:This difference in behavior for different types of passed callable objects may be unexpected (and is it even correct to say that generator-based coroutines are actually considered coroutine objects, as stated in the documentation?). There are three ways to deal with this:
cr_*properties._GeneratorWrapper.The third option has been rejected (see the PR mentioned above). The second option, ironically, may not be very good in terms of performance, especially when
types.coroutine()is used to yield objects to the event loop.Initially,
_GeneratorWrapperwas added in 13f7723 (see #68504; PEP 492). It did not have thecr_*properties, thus replicating only thetypes.GeneratorTypeinterface (the only difference was having the__await__()method). These properties were added in 5376ba9 (see #68588; PEP 492). This was probably done by mistake.VanshAgarwal24036 commentedon Jan 9, 2026
Hi, I’ve opened a PR that fixes this issue by adding the missing coroutine introspection attributes for generator-based coroutines.
picnixz commentedon Jan 9, 2026
@VanshAgarwal24036 Please wait until we decided how to proceed. It's not a trivial matter.