Skip to content
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

Extend contextmanager to asynccontextmanager #11352

Merged
merged 3 commits into from
Oct 27, 2021
Merged

Extend contextmanager to asynccontextmanager #11352

merged 3 commits into from
Oct 27, 2021

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!

mypy/plugins/default.py Outdated Show resolved Hide resolved
test-data/unit/check-default-plugin.test Outdated Show resolved Hide resolved
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!

test-data/unit/check-default-plugin.test Outdated Show resolved Hide resolved
@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