ruff
962e0b72 - [ty] Improve `isinstance()` reachability analysis (#24077)

Commit
42 days ago
[ty] Improve `isinstance()` reachability analysis (#24077) ## Summary Our reachability analysis for `isinstance()` constraints currently doesn't match the sophistication of our type narrowing in this situation. This was obscured in our tests by the fact that `assert_never()`, `reveal_type` and `assert_type` calls can in fact create their own reachability constraints! `assert_type`, `reveal_type` and `assert_never` are all generic functions that return an object of the same type as the object passed into the function. Following https://github.com/astral-sh/ruff/pull/23419, that means that if you pass an object inferred as having the type `Never` into one of these functions, all code following that call will be inferred as being unreachable. In other words, this test was passing entirely by accident, because of additional reachability constraints introduced by the `reveal-type` and `assert_never` calls! https://github.com/astral-sh/ruff/blob/1aabbbc89160251cf13d3282a4963db84419a3bb/crates/ty_python_semantic/resources/mdtest/exhaustiveness_checking.md?plain=1#L470-L477 Remove the `else` branch from that test, and you get a false-positive `invalid-return-type` diagnostic complaining that the function can implicitly return `None` -- which is clearly not the case: ```py def h[T: int | str](x: T) -> T: if isinstance(x, int): return x elif isinstance(x, str): return x ``` The fix is to adapt our `isinstance()` special casing so that it has the same sophistication as our type narrowing machinery here, where we understand that `T & ~U & ~S` must resolve to `Never` if `T` is a type variable bound to the union `S | U`. In a similar way, an `else` branch (explicit or implicit) can never be taken in either of the `h` functions above. ## Test Plan Added mdtests that fail on `main`
Author
Parents
Loading