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

Coroutine[Any, Any, None] is not awaitable #9204

Closed
failable opened this issue Oct 11, 2024 · 25 comments
Closed

Coroutine[Any, Any, None] is not awaitable #9204

failable opened this issue Oct 11, 2024 · 25 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working regression

Comments

@failable
Copy link

Version: 1.1.384

Image

import asyncio


async def test() -> int:
    await asyncio.sleep(1)
    print("Good")
    return 1


async def main():
    await test()


if __name__ == "__main__":
    asyncio.run(main())

@erictraut
Copy link
Collaborator

I'm not able to repro the problem you're reporting.

Code sample in pyright playground

Can you please verify that if you paste your code sample into a new project with a fresh venv that you get the same behavior? Is it possible that you have a local module named asyncio that you're importing from? Are there any other special configuration settings or steps that are needed to repro the issue?

@erictraut erictraut added bug Something isn't working question Further information is requested labels Oct 11, 2024
@tino
Copy link

tino commented Oct 12, 2024

I have the same issue, also in Zed. However I don't have it when I run my homebrew installed pyright on the same code, so might be something with the version in Zed?

@Yevgnen
Copy link

Yevgnen commented Oct 14, 2024

@tino Also in Zed, have you fixed this?

@KimigaiiWuyi
Copy link

KimigaiiWuyi commented Oct 14, 2024

I have the exact same problem.
And I've noticed that these kinds of errors can randomly appear or randomly disappear without any potential change, sometimes by doing nothing, after a while he'll show up with an error, and similarly, even by doing nothing, After a while, it is no longer flagged as an error.

All asynchronous function calls will randomly report errors or not, even if you don't change anything
This could be a issue about Analyzing Performance?

Image

@BaconPancakes
Copy link

BaconPancakes commented Oct 15, 2024

Also reporting a similar issue. Either all the await keywords will return an error or None. It is intermittent at times. I also can't reproduce in pyright sandbox nor in a fresh .venv, unfortunately.

Python version 3.12.5,
Pylance language server 2024.10.100 (pyright version 1.1.384, commit f2dcf07)

Image

I saw some correlation with nest_asyncio. That is

import nest_asyncio

nest_asyncio.apply()

But it is not a 100% reproducible behavior. I might suspect something to do with it though, given its monkey-patchiness

@erictraut
Copy link
Collaborator

Thanks for the additional clues. These are helpful, so keep them coming!

Is this behavior something that started recently? If so, that would point to a recent code change as the cause.

If you restart the language server, does the problem go away?

Are you able to repro the problem with the command-line version of pyright?

Does anyone have a consistent repro, or is it always intermittent and nondeterministic?

@BaconPancakes
Copy link

Is this behavior something that started recently? If so, that would point to a recent code change as the cause.

Yes. This is very recent. I just rolled back to
Pylance language server 2024.10.1 (pyright version 1.1.382, commit e46efc6b) and don't see the issue yet (though it could easily be intermittently gone). I'll update if it shows up again.

If you restart the language server, does the problem go away?

No, I've also tried developer: reload, clear python cache and reload and pylance: clear persisted indices.

Are you able to repro the problem with the command-line version of pyright?

No dice.

@failable
Copy link
Author

failable commented Oct 15, 2024

Yes, it just randomly happens.

  • When I open a project, it's just fine.
  • When I start editing, the errors show.
  • When I save the file, the errors maybe disappear seconds or not. If they disappears, they are shown after seconds.
  • repeats...

The issue happens in this recent week and there're tons of errors in my projects.

@1hachem
Copy link

1hachem commented Oct 15, 2024

thank god I found this issue, this drove me crazy the last three days

@erictraut
Copy link
Collaborator

And I've noticed that these kinds of errors can randomly appear

When you say "these kinds of errors", are you referring specifically to the "Coroutine[Any, Any, None] is not awaitable" error, or are there other errors you're seeing? In the absence of a solid repro, I'm trying to establish some pattern, so any clues you can provide are welcome.

@KimigaiiWuyi
Copy link

KimigaiiWuyi commented Oct 16, 2024

And I've noticed that these kinds of errors can randomly appear

When you say "these kinds of errors", are you referring specifically to the "Coroutine[Any, Any, None] is not awaitable" error, or are there other errors you're seeing? In the absence of a solid repro, I'm trying to establish some pattern, so any clues you can provide are welcome.

Basically, I mean All asynchronous function calls, i.e. Coroutine[Any, Any, None] is not awaitable.
randomly means, it will mark the wavy lines randomly, or it will mark the wavy lines or unmark the wavy lines after I save file Or let it sit for a while.

@failable
Copy link
Author

For me, "these kinds of errors" are

  •   "Coroutine[Any, Any, list[TextRegion]]" is incompatible with protocol "Awaitable[_T_co@Awaitable]"    "__await__" is an incompatible type      Type "() -> Generator[Any, Any, list[TextRegion]]" is not assignable to type "() -> Generator[Any, Any, _T_co@Awaitable]"        Function return type "Generator[Any, Any, list[TextRegion]]" is incompatible with type "Generator[Any, Any, _T_co@Awaitable]"          "Generator[Any, Any, list[TextRegion]]" is not assignable to "Generator[Any, Any, _T_co@Awaitable]"
  •  "Future[list[Drug | BaseException | None]]" is incompatible with protocol "Awaitable[_T_co@Awaitable]"    "__await__" is an incompatible type      Type "() -> Generator[Any, None, list[Drug | BaseException | None]]" is not assignable to type "() -> Generator[Any, Any, _T_co@Awaitable]"        Function return type "Generator[Any, None, list[Drug | BaseException | None]]" is incompatible with type "Generator[Any, Any, _T_co@Awaitable]"          "Generator[Any, None, list[Drug | BaseException | None]]" is not assignable to "Generator[Any, Any, _T_co@Awaitable]"
  •   "Literal['parse_']" is not assignable to "str"
  •   "builtins.str" is not assignable to "builtins.str"

I had 0 of them before, now I have 539 of them.

@erictraut
Copy link
Collaborator

erictraut commented Oct 16, 2024

I had 0 of them before, now I have 539 of them.

Thanks for the info. When you say "before", do you remember which version you were using?

Has anyone seen the problem with the command-line version of pyright, or is this only with the language server?

I just published version 1.1.385 of pyright to npm. It should be published to pypi in the next 24 hours. The last two errors in your list above are likely addressed by the new version, but I don't think the first two errors will be.

@failable
Copy link
Author

I don't remember since Zed automatically download and update pyright for me every time. And I'm sure these two issues come at the same time in the recent 10-14 days. I've never seem them before.

@1hachem
Copy link

1hachem commented Oct 16, 2024

@erictraut this issue still persist with pyright version 1.1.385 installed

@demux
Copy link

demux commented Oct 16, 2024

I have this issue in vscode with Pylance v2024.10.100 (pre-release)

@AtomicMegaNerd
Copy link

I am seeing this exact same issue in Neovim using pyright installed via Homebrew on my M1 Mac:

❯ pyright --version
pyright 1.1.385
❯ nvim --version
NVIM v0.10.2
Build type: Release
LuaJIT 2.1.1727870382

This just started for me about a week or so ago. These errors around async functions seemingly appear and disappear at random.

My project is using Poetry with a local virtualenv at $PROJECT_ROOT/.venv. Here is the relevant section of my pyproject.toml file:

[tool.pyright]
exclude = [".venv", "build", "dist"]
venvPath = "."
venv = ".venv"

Many errors like this one:

Image

@josiah-lunit
Copy link

Also reporting this problem, specifically after I save or run my linter. The errors go away after editing the file (but not saving).

pyright: 1.1.385
nvim: v0.11.0-nightly+cd8e15e
Build type: Release
LuaJIT 2.1.1713773202

Example diagnostic text:

Coroutine[Any, Any, T | None] is not awaitable
... is incompatible with protocol "Awaitable[_T_co@Awaitable]"
"__await__" is an incompatible type
Type "() -> Generator[Any, Any, T | None] is not assignable to type () -> Generator[Any, Any, _T_co@Awaitable]

@zivanovicb
Copy link

zivanovicb commented Oct 17, 2024

+1, also zed, literally same looking errors as others have reported

@trendels
Copy link

trendels commented Oct 17, 2024

In case it helps, I'm seeing what is probably the same regression, but the reported errors are about assignments of string literals to parameters of type str:

 Diagnostics:
 Argument of type "LiteralString" cannot be assigned to parameter "s" of type "str" in function "write"
   "builtins.str" is not assignable to "builtins.str" [reportArgumentType]

This is on Python 3.12.1, with pyright versions 1.1.384 and 1.1.385. Version 1.1.383 does not show these errors.
The errors are only reported in the editor (neovim with nvim-lspconfig my case), never when running pyright from the command line.
I have not managed to produce a short file to reproduce it, it only happens when I edit a file that is part of a larger codebase. As others have reported, the errors are not shown immediately after opening the file. I have not found a reliable way to trigger it, but for me it seems to require making edits and saving the file before it shows up.

@erictraut
Copy link
Collaborator

Thanks to everyone for the clues. Unfortunately, I haven't been able to find a repro, and I'm running short on theories at this point. I've tried to repro using a bunch of large open-source code bases that make heavy use of await and async IO, but I haven't seen the problem yet.

Are any of you able to repro this with a source base that is publicly accessible in GitHub?

@trendels, you mentioned that you're seeing an error with LiteralString. I don't have any good theories for how that could be caused by the same issue as the "not awaitable" error that others are reporting here. The latter seems to be related to protocol matching, but LiteralString and str have nothing to do with protocols. Do you have a code sample that reliably repros the issue you're seeing with LiteralString?

@KimigaiiWuyi
Copy link

KimigaiiWuyi commented Oct 17, 2024

I've tried to repro using a bunch of large open-source code bases that make heavy use of await and async IO

I don't think the problem seems to be how big the repo is, as issuse suggests, it can be a problem on a very short example, anyway, I'm glad there won't be any problems on pyright 1.1.383

@BaconPancakes
Copy link

BaconPancakes commented Oct 18, 2024

I think it's related to nest_asyncio in some way.

Here's some self-contained code that will show the error on my machine:

import openai
import nest_asyncio
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from openai.types import chat

nest_asyncio.apply() # pyright: ignore[reportUnknownMemberType]


async def complete(prompt: str) -> str:
    client = openai.AsyncOpenAI(
        base_url="",
    )
    message: chat.chat_completion_message_param.ChatCompletionMessageParam = {
        "role": "user",
        "content": prompt,
    }

    response = await client.chat.completions.create(
        model="MY_MODEL",
        messages=[message],
    )
    return str(response.choices[0].message.content)

Image

I'm going to attach a mock repo that contains this code and errors.
https://github.com/BaconPancakes/debug_await_pyright

I initialized via https://docs.astral.sh/uv/ with uv sync and I'm having VSCode point to the generated .venv folder for language server.

  • Pylance language server 2024.10.101 (pyright version 1.1.385, commit 05e8fa0)
  • Operating System: Ubuntu 22.04.4 LTS
  • Kernel: Linux 5.15.0-101-generic
  • Architecture: x86-64

erictraut added a commit that referenced this issue Oct 18, 2024
…ften relating to the "awaitable" check. This addresses #9204.
@erictraut erictraut removed the question Further information is requested label Oct 18, 2024
erictraut added a commit that referenced this issue Oct 18, 2024
…ften relating to the "awaitable" check. This addresses #9204. (#9250)
@erictraut
Copy link
Collaborator

@BaconPancakes, thanks for taking the time to put together those repro steps. I really appreciate it. It took me a while to get a repro, even with your detailed instructions, but I eventually got it to happen. This allowed me to find and fix the underlying cause.

The underlying bug was introduced a few years ago (relating to special-case handling of functools.partial), but the effects of this bug were masked. A recent (seemingly unrelated) change caused this bug to manifest itself. And yes, this bug explains all of the symptoms reported above, including the error involving LiteralString.

Thanks everyone for your help in providing clues. Apologies for the regression and the inconvenience this caused.

This will be addressed in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Oct 18, 2024
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.386

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working regression
Projects
None yet
Development

No branches or pull requests