Skip to content

Commit 4c5b03d

Browse files
authored
[mypyc] Add some async tests (#18956)
Improve test coverage.
1 parent 6646ee0 commit 4c5b03d

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

mypyc/test-data/run-async.test

+77-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
[case testRunAsyncBasics]
44
import asyncio
55

6+
from testutil import assertRaises
7+
68
async def h() -> int:
79
return 1
810

@@ -19,14 +21,57 @@ async def f2() -> int:
1921
x += i + await f() + await g()
2022
return x
2123

22-
def test_1() -> None:
24+
def test_simple_call() -> None:
2325
result = asyncio.run(f())
2426
assert result == 3
2527

26-
def test_2() -> None:
28+
def test_multiple_awaits_in_expression() -> None:
2729
result = asyncio.run(f2())
2830
assert result == 9
2931

32+
class MyError(Exception):
33+
pass
34+
35+
async def exc1() -> None:
36+
await asyncio.sleep(0)
37+
raise MyError()
38+
39+
async def exc2() -> None:
40+
await asyncio.sleep(0)
41+
raise MyError()
42+
43+
async def exc3() -> None:
44+
await exc1()
45+
46+
async def exc4() -> None:
47+
await exc2()
48+
49+
async def exc5() -> int:
50+
try:
51+
await exc1()
52+
except MyError:
53+
return 3
54+
return 4
55+
56+
async def exc6() -> int:
57+
try:
58+
await exc4()
59+
except MyError:
60+
return 3
61+
return 4
62+
63+
def test_exception() -> None:
64+
with assertRaises(MyError):
65+
asyncio.run(exc1())
66+
with assertRaises(MyError):
67+
asyncio.run(exc2())
68+
with assertRaises(MyError):
69+
asyncio.run(exc3())
70+
with assertRaises(MyError):
71+
asyncio.run(exc4())
72+
assert asyncio.run(exc5()) == 3
73+
assert asyncio.run(exc6()) == 3
74+
3075
[file asyncio/__init__.pyi]
3176
async def sleep(t: float) -> None: ...
3277
# eh, we could use the real type but it doesn't seem important
@@ -261,3 +306,33 @@ async def x() -> None:
261306
import asyncio
262307
import native
263308
asyncio.run(native.x())
309+
310+
[case testRunAsyncSpecialCases]
311+
import asyncio
312+
313+
async def t() -> tuple[int, str, str]:
314+
return (1, "x", "y")
315+
316+
async def f() -> tuple[int, str, str]:
317+
return await t()
318+
319+
def test_tuple_return() -> None:
320+
result = asyncio.run(f())
321+
assert result == (1, "x", "y")
322+
323+
async def e() -> ValueError:
324+
return ValueError("foo")
325+
326+
async def g() -> ValueError:
327+
return await e()
328+
329+
def test_exception_return() -> None:
330+
result = asyncio.run(g())
331+
assert isinstance(result, ValueError)
332+
333+
[file asyncio/__init__.pyi]
334+
async def sleep(t: float) -> None: ...
335+
# eh, we could use the real type but it doesn't seem important
336+
def run(x: object) -> object: ...
337+
338+
[typing fixtures/typing-full.pyi]

0 commit comments

Comments
 (0)