Pylance-release: "Awaitable[Any]" is incompatible with "Coroutine[Any, Any, Any]"

Created on 29 Jul 2020  路  3Comments  路  Source: microsoft/pylance-release

Environment data

  • Language Server version: 2020.7.3 (pyright 5004efe5)
  • OS and version: macOS 10.15.6
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.5 via Homebrew

Expected behaviour

Awaitable[Any] should be compatible with Coroutine[Any, Any, Any]. Or, potentially, instead of being an Awaitable[Any] an un-awaited coroutine should be Coroutine[Any, Any, Any]? For comparison, I do not believe mypy views the below code snippet as an error, though it is possible I'm using mypy incorrectly.

Details from mypy are available here: https://mypy.readthedocs.io/en/stable/more_types.html#async-and-await

Actual behaviour

Pylance throws an error:

Argument of type "Awaitable[str]" cannot be assigned to parameter "coro" of type "Coroutine[Any, Any, Any]" in function "inspector"
  "Awaitable[str]" is incompatible with "Coroutine[Any, Any, Any]"Pylance (reportGeneralTypeIssues)

Logs

I don't think logs are necessary for this issue, can provide if requested.

Code Snippet / Additional information

Here's a silly example that just does some basic introspection of a passed coroutine. It runs as-is if copied into ipython, but pylance flags with the error from above on the last line, inner(1, "test"). I don't have much experience with mypy but I do not believe it is flagging the same line as an error.

import asyncio
from typing import Any, Coroutine

async def inspector(coro: Coroutine[Any, Any, Any]):
    print(coro.cr_frame.f_locals)
    return await coro

async def inner(sleep: int, message: str) -> str:
    await asyncio.sleep(sleep)
    print(message)
    return message

async def outer():
    await inspector(inner(1, "test"))
bug fixed in next version

All 3 comments

Thanks for the bug report. Currently, Pylance infers the type Awaitable[T] for an async function's return type. Mypy appears to infer the type Coroutine[Any, Any, T]. Both are arguably correct because Coroutine derives from Awaitable, but I think it's reasonable to change the behavior to match mypy in this case. This change will be in the next version of Pylance.

This didn't quite make it into 2020.7.4, but will be in the next release.

This issue has been fixed in version 2020.8.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202080-5-august-2020

Was this page helpful?
0 / 5 - 0 ratings