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

Skip to content

Extend contextmanager to asynccontextmanager - #11352

Merged
JelleZijlstra merged 3 commits into
python:masterfrom
BarnabyShearer:asynccontextmanager_generic
Oct 27, 2021
Merged

Extend contextmanager to asynccontextmanager#11352
JelleZijlstra merged 3 commits into
python:masterfrom
BarnabyShearer:asynccontextmanager_generic

Conversation

@BarnabyShearer

Copy link
Copy Markdown
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")
        )

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

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!

Comment thread mypy/plugins/default.py Outdated
Comment thread test-data/unit/check-default-plugin.test Outdated

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

Comment thread test-data/unit/check-default-plugin.test Outdated
@sobolevn

Copy link
Copy Markdown
Member

Sorry, I cannot press "approve workflows".

@BarnabyShearer

Copy link
Copy Markdown
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