3
3
[case testRunAsyncBasics]
4
4
import asyncio
5
5
6
+ from testutil import assertRaises
7
+
6
8
async def h() -> int:
7
9
return 1
8
10
@@ -19,14 +21,57 @@ async def f2() -> int:
19
21
x += i + await f() + await g()
20
22
return x
21
23
22
- def test_1 () -> None:
24
+ def test_simple_call () -> None:
23
25
result = asyncio.run(f())
24
26
assert result == 3
25
27
26
- def test_2 () -> None:
28
+ def test_multiple_awaits_in_expression () -> None:
27
29
result = asyncio.run(f2())
28
30
assert result == 9
29
31
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
+
30
75
[file asyncio/__init__.pyi]
31
76
async def sleep(t: float) -> None: ...
32
77
# eh, we could use the real type but it doesn't seem important
@@ -261,3 +306,33 @@ async def x() -> None:
261
306
import asyncio
262
307
import native
263
308
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