Skip to content

Commit 00d3f82

Browse files
authored
Add a test case for covariant asyncio.Task (#8833)
1 parent ef4e52d commit 00d3f82

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
import asyncio
4+
5+
6+
class Waiter:
7+
def __init__(self) -> None:
8+
self.tasks: list[asyncio.Task[object]] = []
9+
10+
def add(self, t: asyncio.Task[object]) -> None:
11+
self.tasks.append(t)
12+
13+
async def join(self) -> None:
14+
await asyncio.wait(self.tasks)
15+
16+
17+
async def foo() -> int:
18+
...
19+
20+
21+
async def main() -> None:
22+
# asyncio.Task is covariant in its type argument, which is unusual since its parent class
23+
# asyncio.Future is invariant in its type argument. This is only sound because asyncio.Task
24+
# is not actually Liskov substitutable for asyncio.Future: it does not implement set_result.
25+
w = Waiter()
26+
t: asyncio.Task[int] = asyncio.create_task(foo())
27+
w.add(t)
28+
await w.join()

0 commit comments

Comments
 (0)