Open alenzo-arch opened 3 months ago
Funny enough, the following typechecks (note how async
disappears in overload
stubs). This seems to be the same kind of problem as functions/methods and decorators: too much of "indirectly applied" stuff confuses the checker. In your original snippet, the function is "async twice": mypy
thinks that it returns a Coroutine
which, when awaited, produces an AsyncIterator
. But this only happens when applying a decorator to overloaded definition (see the full playground, reveal_type
produces a right type indeed).
from contextlib import asynccontextmanager, contextmanager
from typing import AsyncIterator, Iterator, overload
@overload
@asynccontextmanager
def test_async() -> AsyncIterator[int]: ...
@overload
@asynccontextmanager
def test_async(val: int = ...) -> AsyncIterator[int]: ...
@asynccontextmanager
async def test_async(val: int = 1) -> AsyncIterator[int]:
yield val
(I'm posting this more like a temporary workaround, this definitely looks like a typechecker bug to me, and pyright
has no objections to your snippet)
@sterliakov Thank for for addressing this as well as providing a temporary work around
Bug Report
When trying to annotate overloads for an asynccontextmanager, I think mypy incorrectly reports an invalid return type.
To Reproduce
This gives an error of "Argument 1 to "asynccontextmanager" has incompatible type "Callable[[int], Coroutine[Any, Any, AsyncIterator[int]]]"; expected "Callable[[int], AsyncIterator[Never]]"
Where the synchronous version checks fine.
Expected Behavior
I expect the async version to work the same as the sync version
Actual Behavior
Mypy reports and error
Your Environment
mypy test.py
mypy.ini
(and other config files):