ruff
bfd67d04 - [ty] Preserve excluded types when inferring type variable upper bounds (#30237)

Commit
2 days ago
[ty] Preserve excluded types when inferring type variable upper bounds (#30237) ty could reject `dict.fromkeys(...)` when the expected value type was a union containing a bounded type variable. It could also reject a fresh `dict(rows.items())` passed to `MappingProxyType` when the return annotation supplied a wider key type. Both cases lost a valid specialization while comparing an intersection with an upper bound. For static types `A` and `B`, `(U & ~A) ≤ B` is equivalent to `U ≤ B | A`. Checking the positive element alone gave the solver the stronger constraint `U ≤ B`. This change keeps the excluded types in the upper bound and checks the whole target before splitting a union into its arms. Fixes astral-sh/ty#4851 Fixes astral-sh/ty#4967 ## Test plan Compared the reported examples across ty versions. The `MappingProxyType` example passes in 0.0.98, fails in 0.0.99, and passes with this change. With `unsound-return-statement` enabled, the reported `list()` and `list((None,))` constructors also infer the annotated element type.
Author
Parents
Loading