Skip to content

types.GeneratorType.gi_yieldfrom is not correct #14333

Closed
@bzoracler

Description

@bzoracler

typeshed/stdlib/types.pyi

Lines 390 to 391 in d3cfe67

@property
def gi_yieldfrom(self) -> GeneratorType[_YieldT_co, _SendT_contra, Any] | None: ...

It's relatively simple to demonstrate counterexamples that are not GeneratorType objects. For example,

Iterator:

from collections.abc import Generator
from types import GeneratorType

def gf() -> Generator[int]:
    yield from [1, 2, 3, 4]

gen = gf()
assert type(gen) is GeneratorType
next(gen)  # 1
print(type(gen.gi_yieldfrom))  # <class 'list_iterator'>

builtins.coroutine_wrapper (some kind of collections.abc.Generator?):

# python 3.10
import asyncio

async def a():
    await asyncio.sleep(4)
    return 1

def gf():
    yield from a().__await__()

gen = gf()
next(gen)  # _asyncio.Future object
print(type(gen.gi_yieldfrom))  # <class 'coroutine_wrapper'>

So, what would be a useful type annotation for this that is still accurate? Would Iterator[Any] | None be OK, or do we have to do the same thing as types.CoroutineType.cr_await and erase all useful information for the type?

typeshed/stdlib/types.pyi

Lines 438 to 442 in d3cfe67

class CoroutineType(Coroutine[_YieldT_co, _SendT_contra, _ReturnT_co]):
__name__: str
__qualname__: str
@property
def cr_await(self) -> Any | None: ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions