Skip to content

Add a test case for covariant asyncio.Task #8833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test_cases/stdlib/asyncio/check_task.py
Original file line number Diff line number Diff line change
@@ -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()