Skip to content

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

@gwax

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)

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