python / python/mypy

@overload doesn't work with @asynccontextmanager

Open
#17,340 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When trying to annotate overloads for an asynccontextmanager, I think mypy incorrectly reports an invalid return type.

To Reproduce

from contextlib import asynccontextmanager, contextmanager
from typing import AsyncIterator, Iterator, overload

@overload
@asynccontextmanager
async def test_async() -> AsyncIterator[int]: ...


@overload
@asynccontextmanager
async def test_async(val: int = ...) -> AsyncIterator[int]: ...


@asynccontextmanager
async def test_async(val: int = 1) -> AsyncIterator[int]:
    yield val


@overload
@contextmanager
def test_sync() -> Iterator[int]: ...


@overload
@contextmanager
def test_sync(val: int = ...) -> Iterator[int]: ...


@contextmanager
def test_sync(val: int = 1) -> Iterator[int]:
    yield val

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

mypy test.py
test.py:5: error: Argument 1 to "asynccontextmanager" has incompatible type "Callable[[], Coroutine[Any, Any, AsyncIterator[int]]]"; expected "Callable[[], AsyncIterator[Never]]"  [arg-type]
test.py:10: error: Argument 1 to "asynccontextmanager" has incompatible type "Callable[[int], Coroutine[Any, Any, AsyncIterator[int]]]"; expected "Callable[[int], AsyncIterator[Never]]"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: mypy test.py
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.11.4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided test.py reproduction with mypy and compare the asynccontextmanager and contextmanager cases. Trace the type checking for these decorators, then add a regression test covering overloaded asynccontextmanager functions and verify that mypy reports no errors for the corrected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.