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.sleep() should have an overload #7866

Closed
wch opened this issue May 18, 2022 · 1 comment · Fixed by #7873
Closed

asyncio.sleep() should have an overload #7866

wch opened this issue May 18, 2022 · 1 comment · Fixed by #7873

Comments

@wch
Copy link
Contributor

wch commented May 18, 2022

This is from microsoft/pyright#3475.

With the latest version of pyright (1.1.247), this code results in an error:

from typing import Awaitable, TypeVar
import asyncio
T = TypeVar("T")

async def wrap(coro: Awaitable[T]) -> T:
    return await coro

async def wrap_sleep() -> None:
    await wrap(asyncio.sleep(0))

The result when I run pyright on it:

Found 1 source file
/..../test/test.py
  /..../test/test.py:9:16 - error: Argument type is partially unknown
    Argument corresponds to parameter "coro" in function "wrap"
    Argument type is "Coroutine[Any, Any, Unknown]" (reportUnknownArgumentType)
1 error, 0 warnings, 0 informations 

The fix, as suggested by @JelleZijlstra and @erictraut, is to add an overload like this:

 async def sleep(delay: float) -> None: ...
@srittau
Copy link
Collaborator

srittau commented May 18, 2022

Sounds right, PR welcome!

Side note: This could benefit from python/typing#307.

wch added a commit to wch/typeshed that referenced this issue May 18, 2022
JelleZijlstra pushed a commit that referenced this issue May 18, 2022
Closes #7866. This adds an overload to `asyncio.sleep()`, so that when it is called _without_ `return=None`, the type checker knows that the return type is `None` instead of `unknown`.

Also related to microsoft/pyright#3475.
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

Successfully merging a pull request may close this issue.

2 participants