-
Notifications
You must be signed in to change notification settings - Fork 226
Closed
astral-sh/ruff
#23107Labels
bidirectional inferenceInference of types that takes into account the context of a declared type or expected typeInference of types that takes into account the context of a declared type or expected type
Milestone
Description
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)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bidirectional inferenceInference of types that takes into account the context of a declared type or expected typeInference of types that takes into account the context of a declared type or expected type