Skip to content

AsyncGenerator/AsyncIterable do not work with overloaded async def #14996

Closed

Description

@gwax

Bug Report

When using typing.overload with an async function that returns an AsyncGenerator or AsyncIterable, the overloads fail with Overloaded function implementation cannot produce return type of signature # when defined using async def

To Reproduce

Failure case: https://mypy-play.net/?mypy=latest&python=3.11&gist=f9b64edbf09acd4b1952f5753669c2e7

from typing import AsyncGenerator, overload

@overload
async def foo(*, a: int) -> AsyncGenerator[int, None]:
    ...
    
@overload
async def foo(*, b: int) -> AsyncGenerator[int, None]:
    ...

async def foo(*, a: int | None = None, b: int | None = None) -> AsyncGenerator[int, None]:
    if a is not None:
        for i in range(a):
            yield i
    elif b is not None:
        for i in range(b):
            yield i
    else:
        yield -1

Unexpected success case: https://mypy-play.net/?mypy=latest&python=3.11&gist=2fcc9c2372664f4ce257a9cec2849698

from typing import AsyncGenerator, overload

@overload
def foo(*, a: int) -> AsyncGenerator[int, None]:
    ...
    
@overload
def foo(*, b: int) -> AsyncGenerator[int, None]:
    ...

async def foo(*, a: int | None = None, b: int | None = None) -> AsyncGenerator[int, None]:
    if a is not None:
        for i in range(a):
            yield i
    elif b is not None:
        for i in range(b):
            yield i
    else:
        yield -1

Expected Behavior

no errors when overloads are also defined with async def and error when overloads are defined with def

Actual Behavior

when overloads are defined with async def:

main.py:11: error: Overloaded function implementation cannot produce return type of signature 1  [misc]
main.py:11: error: Overloaded function implementation cannot produce return type of signature 2  [misc]
Found 2 errors in 1 file (checked 1 source file)

when overloads are defined with def: no errors

Your Environment

  • Mypy version used: 1.1.1 (mypy-play)
  • Python version used: 3.11 (mypy-play)

Activity

  1. added
    bugmypy got something wrong
    on Apr 2, 2023
  2. hauntsaninja commented on Apr 2, 2023

    @hauntsaninja
    Collaborator

    Your success case is expected and is the recommend thing to do. I just added some more documentation for this last week in #14973, see https://mypy.readthedocs.io/en/latest/more_types.html#asynchronous-iterators

  3. added a commit that references this issue on Apr 2, 2023
    178b6ca
  4. added a commit that references this issue on Apr 3, 2023
    7d2844c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-asyncasync, await, asyncio

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions