ruff
416e956f - [ty] Infer better specializations of unions with `None` (etc) (#20749)

Commit
65 days ago
[ty] Infer better specializations of unions with `None` (etc) (#20749) This PR adds a specialization inference special case that lets us handle the following examples better: ```py def f[T](t: T | None) -> T: ... def g[T](t: T | int | None) -> T | int: ... def _(x: str | None): reveal_type(f(x)) # revealed: str (previously str | None) def _(y: str | int | None): reveal_type(g(x)) # revealed: str | int (previously str | int | None) ``` We already have a special case for when the formal is a union where one element is a typevar, but it maps the entire actual type to the typevar (as you can see in the "previously" results above). The new special case kicks in when the actual is also a union. Now, we filter out any actual union elements that are already subtypes of the formal, and only bind whatever types remain to the typevar. (The `| None` pattern appears quite often in the ecosystem results, but it's more general and works with any number of non-typevar union elements.) The new constraint solver should handle this case as well, but it's worth adding this heuristic now with the old solver because it eliminates some false positives from the ecosystem report, and makes the ecosystem report less noisy on the other constraint solver PRs.
Author
Parents
Loading