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

Pyright does not handle NoReturn in async await #2598

Closed
kumaraditya303 opened this issue Nov 23, 2021 · 7 comments
Closed

Pyright does not handle NoReturn in async await #2598

kumaraditya303 opened this issue Nov 23, 2021 · 7 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@kumaraditya303
Copy link

Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues.

Describe the bug
Pyright does not handle NoReturn in async await

Expected behavior
No error.

Screenshots or Code
If applicable, add screenshots or the text of the code (surrounded by triple back ticks) to help explain your problem.

# pyright: strict
import asyncio
from typing import NoReturn


async def func() -> NoReturn:
    while True:
        await asyncio.sleep(1)
        print("Hello World!")


async def main() -> NoReturn:
    await func()
    # <--- This will never execute 


asyncio.run(main())

VS Code extension or command-line
Are you running pyright as a VS Code extension or a command-line tool? Which version? You can find the version of the VS Code extension by clicking on the Pyright icon in the extensions panel.

pyright 1.1.189
@erictraut erictraut added the bug Something isn't working label Nov 23, 2021
@kumaraditya303
Copy link
Author

Same happens with generators too:

# pyright: strict
from typing import Generator, NoReturn


def func() -> Generator[int, None, NoReturn]:
    c = 0
    while True:
        c += 1
        yield c


def main() -> NoReturn:
    for i in func():
        print(i)
    # <--- This will never execute 

main()

@erictraut
Copy link
Collaborator

I've fixed the issue with async functions.

I'm not convinced the second one is valid though. The NoReturn type is not supposed to be used as a type argument. Mypy generates the same error in this case. I think you'll need to use a workaround in the case of a generator.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Nov 23, 2021
@kumaraditya303
Copy link
Author

I'm not convinced the second one is valid though. The NoReturn type is not supposed to be used as a type argument. Mypy generates the same error in this case. I think you'll need to use a workaround in the case of a generator.

If that's the case then why in async function the type can be () -> Coroutine[Any, Any, NoReturn] but in generator it cannot be () -> Generator[int, None, NoReturn] ?

@erictraut
Copy link
Collaborator

NoReturn should not be used as a type argument, so Coroutine[Any, Any, NoReturn] is technically not permitted, but it is legal to use async func() -> NoReturn.

@kumaraditya303
Copy link
Author

It still looks wrong to me that NoReturn cannot be used in case of generators, I have started a discussion on it in typing repo
python/typing#960

@erictraut
Copy link
Collaborator

This is included in pyright 1.1.190, which I just published. It will also be included in the next release of pylance.

@BRIZINGR007
Copy link

BRIZINGR007 commented Jun 4, 2024

Hi Guys , You can create a asyncio.create_task and await that task. With this the Pyright does not throw <"NoReturn" is not awaitable>.

response = asyncio.create_task( awaitable ).
response = await response

Below is an example I solved for aioboto3 for sqs polling :

response = asyncio.create_task( async_sqs_client.receive_message( QueueUrl=self.queue_url, MaxNumberOfMessages=batch_size_of_messages, WaitTimeSeconds=max_polling_duration ) )
response = await response

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
Projects
None yet
Development

No branches or pull requests

3 participants