diff --git a/mypyc/test-data/run-async.test b/mypyc/test-data/run-async.test index 58b690a944af..11ce67077270 100644 --- a/mypyc/test-data/run-async.test +++ b/mypyc/test-data/run-async.test @@ -561,3 +561,85 @@ def test_bool() -> None: [file asyncio/__init__.pyi] def run(x: object) -> object: ... + +[case testRunAsyncNestedFunctions] +import asyncio +from typing import cast, Iterator + +from testutil import assertRaises + +def normal_contains_async_def(x: int) -> int: + async def f(y: int) -> int: + return x + y + + return 5 + cast(int, asyncio.run(f(6))) + +def test_def_contains_async_def() -> None: + assert normal_contains_async_def(3) == 14 + +async def inc(x: int) -> int: + return x + 1 + +async def async_def_contains_normal(x: int) -> int: + def nested(y: int, z: int) -> int: + return x + y + z + + a = x + a += nested((await inc(3)), (await inc(4))) + return a + +def test_async_def_contains_normal() -> None: + assert normal_contains_async_def(2) == (2 + 2 + 4 + 5) + +async def async_def_contains_async_def(x: int) -> int: + async def f(y: int) -> int: + return (await inc(x)) + (await inc(y)) + + return (await f(1)) + (await f(2)) + +def test_async_def_contains_async_def() -> None: + assert asyncio.run(async_def_contains_async_def(3)) == (3 + 1 + 1 + 1) + (3 + 1 + 2 + 1) + +async def async_def_contains_generator(x: int) -> tuple[int, int, int]: + def gen(y: int) -> Iterator[int]: + yield x + 1 + yield x + y + + it = gen(4) + res = x + 10, next(it), next(it) + + with assertRaises(StopIteration): + next(it) + + return res + +def test_async_def_contains_generator() -> None: + assert asyncio.run(async_def_contains_generator(3)) == (13, 4, 7) + +def generator_contains_async_def(x: int) -> Iterator[int]: + async def f(y: int) -> int: + return (await inc(x)) + (await inc(y)) + + yield cast(int, asyncio.run(f(2))) + yield cast(int, asyncio.run(f(3))) + yield x + 10 + +def test_generator_contains_async_def() -> None: + assert list(generator_contains_async_def(5)) == [6 + 3, 6 + 4, 15] + +async def async_def_contains_two_nested_functions(x: int, y: int) -> tuple[int, int]: + def f(a: int) -> int: + return x + a + + def g(b: int, c: int) -> int: + return y + b + c + + return (await inc(f(3))), (await inc(g(4, 10))) + +def test_async_def_contains_two_nested_functions() -> None: + assert asyncio.run(async_def_contains_two_nested_functions(5, 7)) == ( + (5 + 3 + 1), (7 + 4 + 10 + 1) + ) + +[file asyncio/__init__.pyi] +def run(x: object) -> object: ...