From 00d3f8207a76362db18718d6e125aad829e1759f Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Mon, 3 Oct 2022 15:17:49 -0700 Subject: [PATCH] Add a test case for covariant asyncio.Task (#8833) --- test_cases/stdlib/asyncio/check_task.py | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test_cases/stdlib/asyncio/check_task.py diff --git a/test_cases/stdlib/asyncio/check_task.py b/test_cases/stdlib/asyncio/check_task.py new file mode 100644 index 000000000..d674c44ab --- /dev/null +++ b/test_cases/stdlib/asyncio/check_task.py @@ -0,0 +1,28 @@ +from __future__ import annotations + +import asyncio + + +class Waiter: + def __init__(self) -> None: + self.tasks: list[asyncio.Task[object]] = [] + + def add(self, t: asyncio.Task[object]) -> None: + self.tasks.append(t) + + async def join(self) -> None: + await asyncio.wait(self.tasks) + + +async def foo() -> int: + ... + + +async def main() -> None: + # asyncio.Task is covariant in its type argument, which is unusual since its parent class + # asyncio.Future is invariant in its type argument. This is only sound because asyncio.Task + # is not actually Liskov substitutable for asyncio.Future: it does not implement set_result. + w = Waiter() + t: asyncio.Task[int] = asyncio.create_task(foo()) + w.add(t) + await w.join()