Skip to content

Extend contextmanager to asynccontextmanager #11352

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

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/plugins/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_function_hook(self, fullname: str
) -> Optional[Callable[[FunctionContext], Type]]:
from mypy.plugins import ctypes

if fullname == 'contextlib.contextmanager':
if fullname in ('contextlib.contextmanager', 'contextlib.asynccontextmanager'):
return contextmanager_callback
elif fullname == 'builtins.open' and self.python_version[0] == 3:
return open_callback
Expand Down
37 changes: 37 additions & 0 deletions test-data/unit/check-default-plugin.test
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,43 @@ f = g # E: Incompatible types in assignment (expression has type "Callable[[Any,
[typing fixtures/typing-medium.pyi]
[builtins fixtures/tuple.pyi]

[case testAsyncContextManagerWithGenericFunction]
# flags: --python-version 3.7
from contextlib import asynccontextmanager
from typing import TypeVar, AsyncGenerator

T = TypeVar('T')

@asynccontextmanager
async def yield_id(item: T) -> AsyncGenerator[T, None]:
yield item

reveal_type(yield_id) # N: Revealed type is "def [T] (item: T`-1) -> typing.AsyncContextManager[T`-1]"

async with yield_id(1) as x:
reveal_type(x) # N: Revealed type is "builtins.int*"
[typing fixtures/typing-async.pyi]
[builtins fixtures/tuple.pyi]

[case testAsyncContextManagerWithGenericFunctionAndSendType]
# flags: --python-version 3.7
from contextlib import asynccontextmanager
from typing import TypeVar, AsyncGenerator

T = TypeVar('T')
S = TypeVar('S')

@asynccontextmanager
async def yield_id(item: T) -> AsyncGenerator[T, S]:
yield item

reveal_type(yield_id) # N: Revealed type is "def [T, S] (item: T`-1) -> typing.AsyncContextManager[T`-1]"

async with yield_id(1) as x:
reveal_type(x) # N: Revealed type is "builtins.int*"
[typing fixtures/typing-async.pyi]
[builtins fixtures/tuple.pyi]

[case testContextManagerWithUnspecifiedArguments]
from contextlib import contextmanager
from typing import Callable, Iterator
Expand Down