[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.
This commit is contained in:
Ibraheem Ahmed
2025-09-17 18:51:50 -04:00
committed by GitHub
parent bfb0902446
commit e84d523bcf
16 changed files with 341 additions and 78 deletions

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")