Skip to content

Incorrect inferred type for list comprehension of asyncio.create_task #2664

@SebbyLaw

Description

@SebbyLaw

Summary

https://play.ty.dev/b463952d-cb88-4663-b75b-1761c1fb9f19

import asyncio
import random

from typing import Literal, reveal_type


async def get_result() -> Literal["A", "B", "C"]:
    return random.choice(["A", "B", "C"])


# correct inference:
async def process_one():
    task = asyncio.create_task(get_result())

    reveal_type(task)  # `Task[Literal["A", "B", "C"]]`
    result = await task
    reveal_type(result)  # `Literal["A", "B", "C"]`


# incorrect inference:
async def process_many():
    callbacks = [asyncio.create_task(get_result()) for _ in range(5)]
    reveal_type(callbacks)  # `list[Unknown | Task[str]]`
    for callback in callbacks:
        reveal_type(callback)  # `Unknown | Task[str]`
        value = await callback
        reveal_type(value)  # `Unknown | str`

Version

ty 0.0.14 (16597f5 2026-01-26)

Metadata

Metadata

Assignees

Labels

bidirectional inferenceInference of types that takes into account the context of a declared type or expected type

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions