Skip to content

Incompatible return types in overload #4085

@Daenyth

Description

@Daenyth

I'm trying to provide a typed interface for async_contextmanager from asyncio_extras in my codebase, and I thought I'd use an overload to address the issue of being arity-agnostic.

I tried this:

from typing import AsyncContextManager, AsyncIterator, Callable, TypeVar, overload
from asyncio_extras import async_contextmanager as _async_contextmanager  # type: ignore

A = TypeVar('A')
B = TypeVar('B')
R = TypeVar('R')

@overload
def async_contextmanager(f: Callable[[A], AsyncIterator[R]]) -> Callable[[A], AsyncContextManager[R]]:
    pass
@overload
def async_contextmanager(f: Callable[[A, B], AsyncIterator[R]]) -> Callable[[A, B], AsyncContextManager[R]]:
    pass
def async_contextmanager(f):
    return _async_contextmanager(f)

And I get

obs/async_contextmanager.py:14: error: Overloaded function signatures 1 and 2 overlap with incompatible return types
obs/async_contextmanager.py:23: error: Function is missing a type annotation

How can I provide an accurate signature here?

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