Skip to content

Return type of asyncio.gather with *args #11123

@abionics

Description

@abionics

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy