Files
ruff/crates/ty_python_semantic/resources/mdtest/literal/collections/dictionary.md
David Peter 73107a083c [ty] Type inference for comprehensions (#20962)
## Summary

Adds type inference for list/dict/set comprehensions, including
bidirectional inference:

```py
reveal_type({k: v for k, v in [("a", 1), ("b", 2)]})  # dict[Unknown | str, Unknown | int]

squares: list[int | None] = [x for x in range(10)]
reveal_type(squares)  # list[int | None]
```

## Ecosystem impact

I did spot check the changes and most of them seem like known
limitations or true positives. Without proper bidirectional inference,
we saw a lot of false positives.

## Test Plan

New Markdown tests
2025-11-02 14:35:33 +01:00

974 B

Dictionaries

Empty dictionary

reveal_type({})  # revealed: dict[Unknown, Unknown]

Basic dict

reveal_type({1: 1, 2: 1})  # revealed: dict[Unknown | int, Unknown | int]

Dict of tuples

reveal_type({1: (1, 2), 2: (3, 4)})  # revealed: dict[Unknown | int, Unknown | tuple[int, int]]

Unpacked dict

a = {"a": 1, "b": 2}
b = {"c": 3, "d": 4}

d = {**a, **b}
reveal_type(d)  # revealed: dict[Unknown | str, Unknown | int]

Dict of functions

def a(_: int) -> int:
    return 0

def b(_: int) -> int:
    return 1

x = {1: a, 2: b}
reveal_type(x)  # revealed: dict[Unknown | int, Unknown | ((_: int) -> int)]

Mixed dict

# revealed: dict[Unknown | str, Unknown | int | tuple[int, int] | tuple[int, int, int]]
reveal_type({"a": 1, "b": (1, 2), "c": (1, 2, 3)})

Dict comprehensions

# revealed: dict[int | Unknown, int | Unknown]
reveal_type({x: y for x, y in enumerate(range(42))})