ruff
905b9d7f - [ty] Reachability analysis for `isinstance(…)` branches (#19503)

Commit
142 days ago
[ty] Reachability analysis for `isinstance(…)` branches (#19503) ## Summary Add more precise type inference for a limited set of `isinstance(…)` calls, i.e. return `Literal[True]` if we can be sure that this is the correct result. This improves exhaustiveness checking / reachability analysis for if-elif-else chains with `isinstance` checks. For example: ```py def is_number(x: int | str) -> bool: # no "can implicitly return `None` error here anymore if isinstance(x, int): return True elif isinstance(x, str): return False # code here is now detected as being unreachable ``` This PR also adds a new test suite for exhaustiveness checking. ## Test Plan New Markdown tests ### Ecosystem analysis The removed diagnostics look good. There's [one case](https://github.com/pytorch/vision/blob/f52c4f1afd7dec25cbe7b98bcf1cbc840298e8da/torchvision/io/video_reader.py#L125-L143) where a "true positive" is removed in unreachable code. `src` is annotated as being of type `str`, but there is an `elif isinstance(src, bytes)` branch, which we now detect as unreachable. And so the diagnostic inside that branch is silenced. I don't think this is a problem, especially once we have a "graying out" feature, or a lint that warns about unreachable code.
Author
Parents
Loading