[ty] More precise type inference for dictionary literals (#20523)

## Summary

Extends https://github.com/astral-sh/ruff/pull/20360 to dictionary
literals. This also improves our `TypeDict` support by passing through
nested type context.
This commit is contained in:
Ibraheem Ahmed 2025-09-24 18:12:00 -04:00 committed by GitHub
parent f2cc2f604f
commit bea92c8229
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 265 additions and 120 deletions

View file

@ -139,6 +139,15 @@ reveal_type(n) # revealed: list[Literal[1, 2, 3]]
# error: [invalid-assignment] "Object of type `list[Unknown | str]` is not assignable to `list[LiteralString]`"
o: list[typing.LiteralString] = ["a", "b", "c"]
reveal_type(o) # revealed: list[LiteralString]
p: dict[int, int] = {}
reveal_type(p) # revealed: dict[int, int]
q: dict[int | str, int] = {1: 1, 2: 2, 3: 3}
reveal_type(q) # revealed: dict[int | str, int]
r: dict[int | str, int | str] = {1: 1, 2: 2, 3: 3}
reveal_type(r) # revealed: dict[int | str, int | str]
```
## Incorrect collection literal assignments are complained aobut