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.
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()areFuture[Tuple[_T1]],Future[Tuple[_T1, _T2]], and so on.However, the result of
return await asyncio.gather(*tasks)isList. I.e.:This runs find, printing
[1, 2, 3];await asyncio.gather()produces aList[int].However, mypy complains that:
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.