mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:15:12 +00:00
[red-knot] Infer type of if-expression if test has statically known truthiness (#14048)
## Summary Detecting statically known truthy or falsy test in if expressions (ternary). ## Test Plan new mdtest
This commit is contained in:
parent
487941ea66
commit
34a5d7cb7f
3 changed files with 33 additions and 6 deletions
|
@ -2362,13 +2362,15 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
orelse,
|
||||
} = if_expression;
|
||||
|
||||
self.infer_expression(test);
|
||||
|
||||
// TODO detect statically known truthy or falsy test
|
||||
let test_ty = self.infer_expression(test);
|
||||
let body_ty = self.infer_expression(body);
|
||||
let orelse_ty = self.infer_expression(orelse);
|
||||
|
||||
UnionType::from_elements(self.db, [body_ty, orelse_ty])
|
||||
match test_ty.bool(self.db) {
|
||||
Truthiness::AlwaysTrue => body_ty,
|
||||
Truthiness::AlwaysFalse => orelse_ty,
|
||||
Truthiness::Ambiguous => UnionType::from_elements(self.db, [body_ty, orelse_ty]),
|
||||
}
|
||||
}
|
||||
|
||||
fn infer_lambda_body(&mut self, lambda_expression: &ast::ExprLambda) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue