[ty] Infer more precise types for collection literals (#20360)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

## 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.
This commit is contained in:
Ibraheem Ahmed 2025-09-17 18:51:50 -04:00 committed by GitHub
parent bfb0902446
commit e84d523bcf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 341 additions and 78 deletions

View file

@ -79,6 +79,78 @@ b: tuple[int] = ("foo",)
c: tuple[str | int, str] = ([], "foo")
```
## Collection literal annotations are understood
```toml
[environment]
python-version = "3.12"
```
```py
import typing
a: list[int] = [1, 2, 3]
reveal_type(a) # revealed: list[int]
b: list[int | str] = [1, 2, 3]
reveal_type(b) # revealed: list[int | str]
c: typing.List[int] = [1, 2, 3]
reveal_type(c) # revealed: list[int]
d: list[typing.Any] = []
reveal_type(d) # revealed: list[Any]
e: set[int] = {1, 2, 3}
reveal_type(e) # revealed: set[int]
f: set[int | str] = {1, 2, 3}
reveal_type(f) # revealed: set[int | str]
g: typing.Set[int] = {1, 2, 3}
reveal_type(g) # revealed: set[int]
h: list[list[int]] = [[], [42]]
reveal_type(h) # revealed: list[list[int]]
i: list[typing.Any] = [1, 2, "3", ([4],)]
reveal_type(i) # revealed: list[Any | int | str | tuple[list[Unknown | int]]]
j: list[tuple[str | int, ...]] = [(1, 2), ("foo", "bar"), ()]
reveal_type(j) # revealed: list[tuple[str | int, ...]]
k: list[tuple[list[int], ...]] = [([],), ([1, 2], [3, 4]), ([5], [6], [7])]
reveal_type(k) # revealed: list[tuple[list[int], ...]]
l: tuple[list[int], *tuple[list[typing.Any], ...], list[str]] = ([1, 2, 3], [4, 5, 6], [7, 8, 9], ["10", "11", "12"])
reveal_type(l) # revealed: tuple[list[int], list[Any | int], list[Any | int], list[str]]
type IntList = list[int]
m: IntList = [1, 2, 3]
reveal_type(m) # revealed: list[int]
# TODO: this should type-check and avoid literal promotion
# error: [invalid-assignment] "Object of type `list[int]` is not assignable to `list[Literal[1, 2, 3]]`"
n: list[typing.Literal[1, 2, 3]] = [1, 2, 3]
reveal_type(n) # revealed: list[Literal[1, 2, 3]]
# TODO: this should type-check and avoid literal promotion
# error: [invalid-assignment] "Object of type `list[str]` is not assignable to `list[LiteralString]`"
o: list[typing.LiteralString] = ["a", "b", "c"]
reveal_type(o) # revealed: list[LiteralString]
```
## Incorrect collection literal assignments are complained aobut
```py
# error: [invalid-assignment] "Object of type `list[int]` is not assignable to `list[str]`"
a: list[str] = [1, 2, 3]
# error: [invalid-assignment] "Object of type `set[int | str]` is not assignable to `set[int]`"
b: set[int] = {1, 2, "3"}
```
## PEP-604 annotations are supported
```py

View file

@ -46,7 +46,7 @@ def delete():
del d # error: [unresolved-reference] "Name `d` used when not defined"
delete()
reveal_type(d) # revealed: list[@Todo(list literal element type)]
reveal_type(d) # revealed: list[Unknown | int]
def delete_element():
# When the `del` target isn't a name, it doesn't force local resolution.
@ -62,7 +62,7 @@ def delete_global():
delete_global()
# Again, the variable should have been removed, but we don't check it.
reveal_type(d) # revealed: list[@Todo(list literal element type)]
reveal_type(d) # revealed: list[Unknown | int]
def delete_nonlocal():
e = 2

View file

@ -783,9 +783,8 @@ class A: ...
```py
from subexporter import *
# TODO: Should be `list[str]`
# TODO: Should we avoid including `Unknown` for this case?
reveal_type(__all__) # revealed: Unknown | list[@Todo(list literal element type)]
reveal_type(__all__) # revealed: Unknown | list[Unknown | str]
__all__.append("B")

View file

@ -3,7 +3,33 @@
## Empty list
```py
reveal_type([]) # revealed: list[@Todo(list literal element type)]
reveal_type([]) # revealed: list[Unknown]
```
## List of tuples
```py
reveal_type([(1, 2), (3, 4)]) # revealed: list[Unknown | tuple[int, int]]
```
## List of functions
```py
def a(_: int) -> int:
return 0
def b(_: int) -> int:
return 1
x = [a, b]
reveal_type(x) # revealed: list[Unknown | ((_: int) -> int)]
```
## Mixed list
```py
# revealed: list[Unknown | int | tuple[int, int] | tuple[int, int, int]]
reveal_type([1, (1, 2), (1, 2, 3)])
```
## List comprehensions

View file

@ -3,7 +3,33 @@
## Basic set
```py
reveal_type({1, 2}) # revealed: set[@Todo(set literal element type)]
reveal_type({1, 2}) # revealed: set[Unknown | int]
```
## Set of tuples
```py
reveal_type({(1, 2), (3, 4)}) # revealed: set[Unknown | tuple[int, int]]
```
## Set of functions
```py
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
```py
# revealed: set[Unknown | int | tuple[int, int] | tuple[int, int, int]]
reveal_type({1, (1, 2), (1, 2, 3)})
```
## Set comprehensions

View file

@ -310,17 +310,13 @@ no longer valid in the inner lazy scope.
def f(l: list[str | None]):
if l[0] is not None:
def _():
# TODO: should be `str | None`
reveal_type(l[0]) # revealed: str | None | @Todo(list literal element type)
# TODO: should be of type `list[None]`
reveal_type(l[0]) # revealed: str | None | Unknown
l = [None]
def f(l: list[str | None]):
l[0] = "a"
def _():
# TODO: should be `str | None`
reveal_type(l[0]) # revealed: str | None | @Todo(list literal element type)
# TODO: should be of type `list[None]`
reveal_type(l[0]) # revealed: str | None | Unknown
l = [None]
def f(l: list[str | None]):
@ -328,8 +324,7 @@ def f(l: list[str | None]):
def _():
l: list[str | None] = [None]
def _():
# TODO: should be `str | None`
reveal_type(l[0]) # revealed: @Todo(list literal element type)
reveal_type(l[0]) # revealed: str | None
def _():
def _():

View file

@ -9,13 +9,11 @@ A list can be indexed into with:
```py
x = [1, 2, 3]
reveal_type(x) # revealed: list[@Todo(list literal element type)]
reveal_type(x) # revealed: list[Unknown | int]
# TODO reveal int
reveal_type(x[0]) # revealed: @Todo(list literal element type)
reveal_type(x[0]) # revealed: Unknown | int
# TODO reveal list[int]
reveal_type(x[0:1]) # revealed: list[@Todo(list literal element type)]
reveal_type(x[0:1]) # revealed: list[Unknown | int]
# error: [invalid-argument-type]
reveal_type(x["a"]) # revealed: Unknown

View file

@ -55,8 +55,7 @@ def f(x: Iterable[int], y: list[str], z: Never, aa: list[Never], bb: LiskovUncom
reveal_type(tuple((1, 2))) # revealed: tuple[Literal[1], Literal[2]]
# TODO: should be `tuple[Literal[1], ...]`
reveal_type(tuple([1])) # revealed: tuple[@Todo(list literal element type), ...]
reveal_type(tuple([1])) # revealed: tuple[Unknown | int, ...]
# error: [invalid-argument-type]
reveal_type(tuple[int]([1])) # revealed: tuple[int]

View file

@ -213,9 +213,8 @@ reveal_type(d) # revealed: Literal[2]
```py
a, b = [1, 2]
# TODO: should be `int` for both `a` and `b`
reveal_type(a) # revealed: @Todo(list literal element type)
reveal_type(b) # revealed: @Todo(list literal element type)
reveal_type(a) # revealed: Unknown | int
reveal_type(b) # revealed: Unknown | int
```
### Simple unpacking