Skip to content

what's the correct type for types.coroutine decorated generators? #2907

Closed
@toejough

Description

@toejough

I cannot seem to get a types.coroutine decorated generator to typecheck correctly.

I checked the tests out, and they seem to indicate that the right thing to do is to annotate the return as "typing.Generator", but when I do that, mypy complains that I cannot await that.

coro_example.py:

"""Coro typing example."""


import types
import typing


async def await_decorated() -> None:
    """Await a decorated generator."""
    x = await decorated_generator()


@types.coroutine
def decorated_generator() -> typing.Generator:
    """Yield None."""
    yield None
> mypy coro_example.py
coro_example.py:10: error: Incompatible types in await (actual type Generator[Any, Any, Any], expected type "Awaitable")

I thought perhaps I was subtly mistyping something, so I copy/pasted the code from the tests (from line 346 to line 403) and ran that, and got similar errors:

> mypy mypy_example.py
mypy_example.py:45: error: "yield from" can't be applied to Awaitable[int]
mypy_example.py:47: error: "yield from" can't be applied to AwaitableGenerator[Any, Any, int, Awaitable[int]]
mypy_example.py:49: error: "yield from" can't be applied to "Aw"
mypy_example.py:53: error: Incompatible types in await (actual type Generator[str, None, int], expected type "Awaitable")
mypy_example.py:57: error: Incompatible types in await (actual type "It", expected type "Awaitable")

If I type the generator as a typing.Awaitable instead, mypy tells me I have to type the generator as a generator or one of its supertypes.

I've tried this with python 3.6 and the latest github and pip versions:

  • python 3.6.0
  • mypy mypy (0.480.dev0) (installed via pip install git+https://github.com/python/mypy)
  • mypy mypy (0.471) (installed via pip install mypy)

Am I doing it wrong, or is there a bug?

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