Skip to content

Incompatible generic types with asynccontextmanager #11660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
NicholasJohnstone opened this issue Dec 3, 2021 · 1 comment
Closed

Incompatible generic types with asynccontextmanager #11660

NicholasJohnstone opened this issue Dec 3, 2021 · 1 comment
Labels
bug mypy got something wrong

Comments

@NicholasJohnstone
Copy link

NicholasJohnstone commented Dec 3, 2021

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
@NicholasJohnstone NicholasJohnstone added the bug mypy got something wrong label Dec 3, 2021
@hauntsaninja
Copy link
Collaborator

I think fixed on master by #11352

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants