Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Incompatible generic types with asynccontextmanager #11660

Description

@NicholasJohnstone

Bug Report
Mypy reports an incompatible type error when using a generic asynccontextmanager. When using the non-async equivalent there is no error.

To Reproduce

from typing import Type, TypeVar, AsyncIterator, Iterator, Generic
import contextlib

T = TypeVar("T")


@contextlib.contextmanager
def create(my_type: Type[T]) -> Iterator[T]:
    yield my_type()


@contextlib.asynccontextmanager
async def async_create(my_type: Type[T]) -> AsyncIterator[T]:
    yield my_type()


class Foo(Generic[T]):
    def do_something(self, t: T) -> None:
        pass

    def example(self, my_type: Type[T]) -> None:
        with create(my_type) as value1:
            self.do_something(value1)

    async def async_example(self, my_type: Type[T]) -> None:
        async with async_create(my_type) as value2:
            self.do_something(value2)

Mypy reports error: Incompatible types in assignment (expression has type "T", variable has type "T") for the self.do_something(value2) line.

Expected Behavior
No error for the above example

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.7
  • Operating system and version: Ubuntu 20.04

Activity

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 wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions