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

Return type of asyncio.gather with *args #11123

Closed
abionics opened this issue Dec 8, 2023 · 2 comments
Closed

Return type of asyncio.gather with *args #11123

abionics opened this issue Dec 8, 2023 · 2 comments

Comments

@abionics
Copy link

abionics commented Dec 8, 2023

There are some related issues:

But the problem is still the same. When a generator is passed to asyncio.gather, the expected return type should be Future[list[_T]]. However, it currently returns Future[tuple[_T1]], leading to type hinting warnings. This is a common pattern in asyncio code, and every time it cause unnecessary warnings in IDEs

Example code snippet:

async def fetch(url: str) -> str:
    pass  # fetch logic


def analyze(responses: list[str]) -> int:
    pass  # analyze logic that need all responses, maybe external library


async def create_report(urls: list[str]):
    responses = await asyncio.gather(*(
        fetch(url)
        for url in urls
    ))
    some_value = analyze(responses)
    # create report

gather

I read tasks.pyi file, but a significant number of developers use asyncio.gather(*[...]). So this behavior should be reviewed

Although practicality beats purity
(c) The Zen of Python

@JelleZijlstra
Copy link
Member

Which type checker are you using? It seems to me that your code should match this overload:

def gather(*coros_or_futures: _FutureLike[_T], return_exceptions: Literal[False] = False) -> Future[list[_T]]: ... # type: ignore[overload-overlap]
which returns Future[list[_T]]. If your type checker instead picks the overload that returns a 1-tuple, then that should be fixed in the type checker.

I'll also rename this issue to be more focused.

@JelleZijlstra JelleZijlstra changed the title Finally solve issue with asyncio.gather Return type of asyncio.gather with *args Dec 8, 2023
@abionics
Copy link
Author

abionics commented Dec 8, 2023

I use PyCharm's built-in type checker. I run mypy and pyright type checkers on this code snippet and both return 0 errors. So it must be bug in PyCharm

@JelleZijlstra thank you! Your question was the answer :) I think you can close this issue

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