Skip to content

AsyncGenerator.aclose() should return Awaitable[None], not Awaitable[yield-type] #2785

@oremanj

Description

@oremanj

typeshed/stdlib/3/typing.pyi lists AsyncGenerator.aclose() as returning an awaitable that evaluates to the yield type of the generator, same as asend() or athrow(). I think it should return an awaitable evaluating to None, based on the definition of close() in PEP 342: (https://www.python.org/dev/peps/pep-0342/#new-generator-method-close)

def close(self):
    try:
        self.throw(GeneratorExit)
    except (GeneratorExit, StopIteration):
        pass
    else:
        raise RuntimeError("generator ignored GeneratorExit")
    # Other exceptions are not caught

and the definition of aclose() in PEP 525:

Implement an aclose method on asynchronous generators returning a special awaitable. When awaited it throws a GeneratorExit into the suspended generator and iterates over it until either a GeneratorExit or a StopAsyncIteration occur. This is very similar to what the close() method does to regular Python generators, except that an event loop is required to execute aclose().

and the definition of aclose() in collections.abc.AsyncGenerator:

    async def aclose(self):
        try:
            await self.athrow(GeneratorExit)
        except (GeneratorExit, StopAsyncIteration):
            pass
        else:
            raise RuntimeError("asynchronous generator ignored GeneratorExit")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions