ruff
2b6bd95f - [ty] Preserve literal types when consuming dictionary literals (#29145)

Commit
5 days ago
[ty] Preserve literal types when consuming dictionary literals (#29145) Dictionary literals currently promote their keys and values even when consumed immediately, so a lookup table such as `{"A": "Alpha", "B": "Beta"}[letter]` returns `str` and can fail a literal-union return annotation. Preserve literal types when indexing or iterating over a dictionary literal, or calling its `keys()`, `values()`, or `items()` method directly. This is just expanding to literal dictionaries an existing pattern we already supported for literal lists and tuples. Recover key and value unions from the already-inferred elements, including literal `**` unpacking. Stored dictionaries, nested mutable containers, and saved bound methods keep their ordinary mutable types. Dictionary lookups return the union of the value types. Closes https://github.com/astral-sh/ty/issues/4680 ## Test plan Mdtests cover immediate lookups with literal and nonliteral keys, lookup-table returns, invalid key types, direct iteration and dictionary views, stored views, comprehension and destructuring targets, literal and arbitrary mapping unpacking, duplicate keys, and mutation through nested containers and saved bound methods. ## Ecosystem report For head `a0cb267abac45badbcd2329ee5f5bafad339dbba`, all ten stable diagnostic changes are expected: four false-positive argument errors removed, two useful redundancy warnings added, and four existing messages made more precise. No regression found; CI passed. See the [full ecosystem analysis](https://github.com/astral-sh/ruff/pull/29145#issuecomment-6024243441) for minimized examples and source links.
Author
Parents
Loading