[ty] Reachability analysis for isinstance(…) branches by sharkdp · Pull Request #19503 · astral-sh/ruff (original) (raw)
sharkdp changed the title
[ty] Type inference for [ty] Reachability analysis for isinstance(…) callsisinstance(…) branches
sharkdp marked this pull request as ready for review
sharkdp deleted the david/isinstance-inference branch
UnboundVariable pushed a commit to UnboundVariable/ruff that referenced this pull request
- main: (28 commits)
[ty] highlight the argument in
static_asserterror messages (astral-sh#19426) [ty] Infer single-valuedness for enums based onint/str(astral-sh#19510) [ty] Restructure submodule query aroundFiledependency [ty] MakeModulea Salsa ingredient [ty] Reachability analysis forisinstance(…)branches (astral-sh#19503) [ty] Normalize single-member enums to their instance type (astral-sh#19502) [ty] Invertty_ideandty_projectdependency (astral-sh#19501) [ty] Implement mock language server for testing (astral-sh#19391) [ty] Detect enums if metaclass is a subtype of EnumType/EnumMeta (astral-sh#19481) [ty] perform type narrowing for places markedglobaltoo (astral-sh#19381) [ty] UseThinVecfor sub segments inPlaceExpr(astral-sh#19470) [ty] Splat variadic arguments into parameter list (astral-sh#18996) [flake8-pyi] Skip fix if allUnionmembers areNone(PYI016) (astral-sh#19416) Skip notebook with errors in ecosystem check (astral-sh#19491) [ty] Consistent use of American english (in rules) (astral-sh#19488) [ty] Support iterating over enums (astral-sh#19486) Fix panic for illegalLiteral[…]annotations with inner subscript expressions (astral-sh#19489) Move fix suggestion to subdiagnostic (astral-sh#19464) [ty] Implement non-stdlib stub mapping for classes and functions (astral-sh#19471) [ty] Disallow illegal uses ofClassVar(astral-sh#19483) ...
Conflicts:
crates/ty_ide/src/goto.rs
dcreager added a commit that referenced this pull request
- main:
[ty] Fix narrowing and reachability of class patterns with arguments (#19512)
[ty] Implemented partial support for "find references" language server feature. (#19475)
[
flake8-use-pathlib] Add autofix forPTH101,PTH104,PTH105,PTH121(#19404) [perflint] Parenthesize generator expressions (PERF401) (#19325) [pep8-naming] FixN802false positives forCGIHTTPRequestHandlerandSimpleHTTPRequestHandler(#19432) [pylint] Handle empty comments after line continuation (PLR2044) (#19405) Move concise diagnostic rendering toruff_db(#19398) [ty] highlight the argument instatic_asserterror messages (#19426) [ty] Infer single-valuedness for enums based onint/str(#19510) [ty] Restructure submodule query aroundFiledependency [ty] MakeModulea Salsa ingredient [ty] Reachability analysis forisinstance(…)branches (#19503) [ty] Normalize single-member enums to their instance type (#19502) [ty] Invertty_ideandty_projectdependency (#19501) [ty] Implement mock language server for testing (#19391) [ty] Detect enums if metaclass is a subtype of EnumType/EnumMeta (#19481) [ty] perform type narrowing for places markedglobaltoo (#19381)
AlexWaygood pushed a commit that referenced this pull request
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:
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 unreachableThis 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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})