Skip to content

Commit bdac4bc

Browse files
authored
Fix nested async functions when using TypeVar value restriction (#14705)
Propagate additional function flags in TransformVisitor. Work on #14706.
1 parent 186432d commit bdac4bc

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

mypy/treetransform.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ def copy_function_attributes(self, new: FuncItem, original: FuncItem) -> None:
233233
new.max_pos = original.max_pos
234234
new.is_overload = original.is_overload
235235
new.is_generator = original.is_generator
236+
new.is_coroutine = original.is_coroutine
237+
new.is_async_generator = original.is_async_generator
238+
new.is_awaitable_coroutine = original.is_awaitable_coroutine
236239
new.line = original.line
237240

238241
def visit_overloaded_func_def(self, node: OverloadedFuncDef) -> OverloadedFuncDef:

test-data/unit/check-async-await.test

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,3 +959,45 @@ async def good() -> None:
959959
y = [await foo(x) for x in [1, 2, 3]] # OK
960960
[builtins fixtures/async_await.pyi]
961961
[typing fixtures/typing-async.pyi]
962+
963+
[case testNestedAsyncFunctionAndTypeVarAvalues]
964+
from typing import TypeVar
965+
966+
T = TypeVar('T', int, str)
967+
968+
def f(x: T) -> None:
969+
async def g() -> T:
970+
return x
971+
[builtins fixtures/async_await.pyi]
972+
[typing fixtures/typing-async.pyi]
973+
974+
[case testNestedAsyncGeneratorAndTypeVarAvalues]
975+
from typing import AsyncGenerator, TypeVar
976+
977+
T = TypeVar('T', int, str)
978+
979+
def f(x: T) -> None:
980+
async def g() -> AsyncGenerator[T, None]:
981+
yield x
982+
[builtins fixtures/async_await.pyi]
983+
[typing fixtures/typing-async.pyi]
984+
985+
[case testNestedDecoratedCoroutineAndTypeVarValues]
986+
from typing import Generator, TypeVar
987+
from types import coroutine
988+
989+
T = TypeVar('T', int, str)
990+
991+
def f(x: T) -> None:
992+
@coroutine
993+
def inner() -> Generator[T, None, None]:
994+
yield x
995+
reveal_type(inner) # N: Revealed type is "def () -> typing.AwaitableGenerator[builtins.int, None, None, typing.Generator[builtins.int, None, None]]" \
996+
# N: Revealed type is "def () -> typing.AwaitableGenerator[builtins.str, None, None, typing.Generator[builtins.str, None, None]]"
997+
998+
@coroutine
999+
def coro() -> Generator[int, None, None]:
1000+
yield 1
1001+
reveal_type(coro) # N: Revealed type is "def () -> typing.AwaitableGenerator[builtins.int, None, None, typing.Generator[builtins.int, None, None]]"
1002+
[builtins fixtures/async_await.pyi]
1003+
[typing fixtures/typing-async.pyi]

0 commit comments

Comments
 (0)