Files
ruff/crates/ty_python_semantic/resources/mdtest/literal/collections/set.md
Ibraheem Ahmed e84d523bcf [ty] Infer more precise types for collection literals (#20360)
## Summary

Part of https://github.com/astral-sh/ty/issues/168. Infer more precise types for collection literals (currently, only `list` and `set`). For example,

```py
x = [1, 2, 3] # revealed: list[Unknown | int]
y: list[int] = [1, 2, 3] # revealed: list[int]
```

This could easily be extended to `dict` literals, but I am intentionally limiting scope for now.
2025-09-17 18:51:50 -04:00

622 B

Sets

Basic set

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

Set of tuples

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

Set of functions

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

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

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

Mixed set

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

Set comprehensions

reveal_type({x for x in range(42)})  # revealed: set[@Todo(set comprehension element type)]