|
1 | 1 | # async test cases (compile and run) |
2 | 2 |
|
3 | | -[case testRunAsync] |
| 3 | +[case testRunAsyncBasics] |
4 | 4 | import asyncio |
5 | 5 |
|
6 | 6 | async def h() -> int: |
@@ -34,6 +34,73 @@ def run(x: object) -> object: ... |
34 | 34 |
|
35 | 35 | [typing fixtures/typing-full.pyi] |
36 | 36 |
|
| 37 | +[case testRunAsyncAwaitInVariousPositions] |
| 38 | +import asyncio |
| 39 | + |
| 40 | +async def one() -> int: |
| 41 | + return int() + 1 |
| 42 | + |
| 43 | +async def true() -> bool: |
| 44 | + return bool(int() + 1) |
| 45 | + |
| 46 | +async def branch_await() -> int: |
| 47 | + if await true(): |
| 48 | + return 3 |
| 49 | + return 2 |
| 50 | + |
| 51 | +async def branch_await_not() -> int: |
| 52 | + if not await true(): |
| 53 | + return 3 |
| 54 | + return 2 |
| 55 | + |
| 56 | +def test_branch() -> None: |
| 57 | + assert asyncio.run(branch_await()) == 3 |
| 58 | + assert asyncio.run(branch_await_not()) == 2 |
| 59 | + |
| 60 | +async def assign_local() -> int: |
| 61 | + x = await one() |
| 62 | + return x + 1 |
| 63 | + |
| 64 | +def test_assign_local() -> None: |
| 65 | + assert asyncio.run(assign_local()) == 2 |
| 66 | + |
| 67 | +class C: |
| 68 | + def __init__(self, s: str) -> None: |
| 69 | + self.s = s |
| 70 | + |
| 71 | +async def make_c(s: str) -> C: |
| 72 | + await one() |
| 73 | + return C(s) |
| 74 | + |
| 75 | +async def get_attr(s: str) -> str: |
| 76 | + return (await make_c(s)).s |
| 77 | + |
| 78 | +def test_get_attr() -> None: |
| 79 | + assert asyncio.run(get_attr("foo")) == "foo" |
| 80 | + |
| 81 | +async def concat(s: str, t: str) -> str: |
| 82 | + await one() |
| 83 | + return s + t |
| 84 | + |
| 85 | +async def set_attr1(s: str) -> str: |
| 86 | + c = await make_c("xyz") |
| 87 | + c.s = await concat(s, "!") |
| 88 | + return c.s |
| 89 | + |
| 90 | +async def set_attr2(s: str) -> None: |
| 91 | + (await make_c("xyz")).s = s |
| 92 | + |
| 93 | +def test_set_attr() -> None: |
| 94 | + assert asyncio.run(set_attr1("foo")) == "foo!" |
| 95 | + asyncio.run(set_attr2("foo")) # Just check that it compiles and runs |
| 96 | + |
| 97 | +[file asyncio/__init__.pyi] |
| 98 | +async def sleep(t: float) -> None: ... |
| 99 | +# eh, we could use the real type but it doesn't seem important |
| 100 | +def run(x: object) -> object: ... |
| 101 | + |
| 102 | +[typing fixtures/typing-full.pyi] |
| 103 | + |
37 | 104 |
|
38 | 105 | [case testAsyncWith] |
39 | 106 | from testutil import async_val |
@@ -78,7 +145,6 @@ yields, val = run_generator(async_return()) |
78 | 145 | assert yields == ('foo',) |
79 | 146 | assert val == 'test', val |
80 | 147 |
|
81 | | - |
82 | 148 | [case testAsyncFor] |
83 | 149 | from typing import AsyncIterable, List, Set, Dict |
84 | 150 |
|
|
0 commit comments