Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncio.shield -> expecting Future of Future. #3518

Closed
jonathanslenders opened this issue Dec 2, 2019 · 2 comments
Closed

asyncio.shield -> expecting Future of Future. #3518

jonathanslenders opened this issue Dec 2, 2019 · 2 comments

Comments

@jonathanslenders
Copy link
Contributor

Take the following code:

import asyncio

async def f() -> None:
    # error on line 8: Argument 1 to "shield" has incompatible type "Future[int]";
    #    expected "Union[Future[Future[int]], Generator[Any, None, Future[int]], Awaitable[Future[int]]]"
    f: asyncio.Future[int] = asyncio.Future()
    g: asyncio.Future[int] = await asyncio.shield(f)


    # Fine.
    c: asyncio.Future = asyncio.Future()
    d: asyncio.Future = await asyncio.shield(c)

It's not obvious why we would expect a future of a future.
I don't see anything wrong in the type annotations: https://github.com/python/typeshed/blob/master/stdlib/3/asyncio/tasks.pyi#L87 so maybe a bug in mypy?

@jonathanslenders jonathanslenders changed the title asyncio.shield -> expecting Union. asyncio.shield -> expecting Future of Future. Dec 2, 2019
@jonathanslenders
Copy link
Contributor Author

Closing this. The code above was wrong. It should have been:

    g: int = await asyncio.shield(f)

Interesting though is that if the result of await asyncio.shield(...) is not captured in a variable, mypy expects asyncio.Future[<nothing>] for the input of shield. This means we have to assign the result, even when not used.

@srittau
Copy link
Collaborator

srittau commented Dec 2, 2019

@jonathanslenders python/mypy#8051 might be related to your last paragraph.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants