Skip to content

typecheck failure with parameterized iterator #11375

Closed
@smoofra

Description

@smoofra

mypy-bug.py

from typing import *
import asyncio

T = TypeVar('T')

class Foo(Generic[T]):
    def __iter__(self) -> Iterator[Awaitable[T]]:
        return iter([])

async def bar(foo:Foo[T]):
    # No error
    await asyncio.gather(*iter(foo))  

    # mypy says:
    # No overload variant of "gather" matches argument type "Foo[T]"
    await asyncio.gather(*foo)

async def baz(foo:Foo):
    # also no error
    await asyncio.gather(*foo)

To Reproduce

➜  mypy mypy-bug.py
mypy-bug.py:17: error: No overload variant of "gather" matches argument type "Foo[T]"
mypy-bug.py:17: note: Possible overload variants:
mypy-bug.py:17: note:     def gather(coro_or_future1: Union[Future[Any], Generator[Any, None, Any], Awaitable[Any]], coro_or_future2: Union[Future[Any], Generator[Any, None, Any], Awaitable[Any]], coro_or_future3: Union[Future[Any], Generator[Any, None, Any], Awaitable[Any]], coro_or_future4: Union[Future[Any], Generator[Any, None, Any], Awaitable[Any]], coro_or_future5: Union[Future[Any], Generator[Any, None, Any], Awaitable[Any]], coro_or_future6: Union[Future[Any], Generator[Any, None, Any], Awaitable[Any]], *coros_or_futures: Union[Future[Any], Generator[Any, None, Any], Awaitable[Any]], loop: Optional[AbstractEventLoop] = ..., return_exceptions: bool = ...) -> Future[List[Any]]
mypy-bug.py:17: note:     def [_T1] gather(coro_or_future1: Union[Future[_T1], Generator[Any, None, _T1], Awaitable[_T1]], *, loop: Optional[AbstractEventLoop] = ..., return_exceptions: Literal[False] = ...) -> Future[Tuple[_T1]]
mypy-bug.py:17: note:     <9 more similar overloads not shown, out of 11 total overloads>
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

The second gather call should typecheck just like the first one, as using *foo is essentially the same as *iter(foo).

Note also that the type variable is required to reproduce the bug.

Actual Behavior

Mypy prints an error

Your Environment

  • Mypy version used: mypy 0.812, also reproduces with mypy-0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.8.9
  • Operating system and version: macOS 12.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions