KEMBAR78
Extend contextmanager to asynccontextmanager by BarnabyShearer · Pull Request #11352 · python/mypy · GitHub
Skip to content

Conversation

BarnabyShearer
Copy link
Contributor

Closes #9922

Description

Extending the existing special case for contextlib.contextmanager to contextlib.asynccontextmanager. When used with TypeVar gives much better type.

Test Plan

I hope I have included the unit test you need. My minimal reproduction is:

from contextlib import asynccontextmanager
from typing import AsyncGenerator, Type, TypeVar

R = TypeVar("R")


@asynccontextmanager
async def foo(
    cls: Type[R],
) -> AsyncGenerator[R, None]:
    yield cls()


async def bar() -> int:
    async with foo(int) as foo_int:
        return (
            foo_int  # error: Incompatible return value type (got "R", expected "int")
        )

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sobolevn
Copy link
Member

Sorry, I cannot press "approve workflows".

@BarnabyShearer
Copy link
Contributor Author

Anything else needed?

@JelleZijlstra JelleZijlstra merged commit 1bf701f into python:master Oct 27, 2021
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this pull request Jan 20, 2022
Closes  python#9922

### Description

Extending the existing special case for `contextlib.contextmanager` to `contextlib.asynccontextmanager`. When used with `TypeVar` gives much better type.


## Test Plan

I hope I have included the unit test you need. My minimal reproduction is:

```
from contextlib import asynccontextmanager
from typing import AsyncGenerator, Type, TypeVar

R = TypeVar("R")


@asynccontextmanager
async def foo(
    cls: Type[R],
) -> AsyncGenerator[R, None]:
    yield cls()


async def bar() -> int:
    async with foo(int) as foo_int:
        return (
            foo_int  # error: Incompatible return value type (got "R", expected "int")
        )
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Failed TypeVar substitution by asynccontextmanager

3 participants