8000 Clarification: return type for asyncio.gather()? · Issue #3243 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content
Clarification: return type for asyncio.gather()? #3243
Closed
@bsolomon1124

Description

@bsolomon1124

I've come across a source of confusion for asyncio.gather(). It may very well be user error rather than an incorrect annotation. (Motivated by this Stack Overflow post.)

The annotation return types for gather() are Future[Tuple[_T1]], Future[Tuple[_T1, _T2]], and so on.

However, the result of return await asyncio.gather(*tasks) is List. I.e.:

import asyncio
from typing import List

async def foo(x) -> int:
    await asyncio.sleep(x / 2)
    return x

async def main() -> List[int]:
    return await asyncio.gather(*(foo(i) for i in (1, 2, 3)))

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

This runs find, printing [1, 2, 3]; await asyncio.gather() produces a List[int].

However, mypy complains that:

mypytest.py:9: error: Incompatible types in assignment (expression has type "Tuple[Any, ...]", variable has type "List[int]")

Why does this ambiguity exist? How can I provide a correct type hint for async def main()?


I'm looking at typeshed master branch as of 0602840 fyi + mypy 0.720.

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

      0