[red-knot] Precise inference for identity checks (#14109)

## Summary

Adds more precise type inference for `… is …` and `… is not …` identity
checks in some limited cases where we statically know the answer to be
either `Literal[True]` or `Literal[False]`.

I found this helpful while working on type inference for comparisons
involving intersection types, but I'm not sure if this is at all useful
for real world code (where the answer is most probably *not* statically
known). Note that we already have *type narrowing* for identity tests.
So while we are already able to generate constraints for things like `if
x is None`, we can now — in some limited cases — make an even stronger
conclusion and infer that the test expression itself is `Literal[False]`
(branch never taken) or `Literal[True]` (branch always taken).

## Test Plan

New Markdown tests
This commit is contained in:
David Peter 2024-11-05 19:48:52 +01:00 committed by GitHub
parent 05687285fe
commit 2296627528
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 62 additions and 2 deletions

View file

@ -3388,8 +3388,28 @@ impl<'db> TypeInferenceBuilder<'db> {
ast::CmpOp::NotIn => {
membership_test_comparison(MembershipTestCompareOperator::NotIn)
}
ast::CmpOp::Is => Ok(KnownClass::Bool.to_instance(self.db)),
ast::CmpOp::IsNot => Ok(KnownClass::Bool.to_instance(self.db)),
ast::CmpOp::Is => {
if left.is_disjoint_from(self.db, right) {
Ok(Type::BooleanLiteral(false))
} else if left.is_singleton(self.db)
&& left.is_equivalent_to(self.db, right)
{
Ok(Type::BooleanLiteral(true))
} else {
Ok(KnownClass::Bool.to_instance(self.db))
}
}
ast::CmpOp::IsNot => {
if left.is_disjoint_from(self.db, right) {
Ok(Type::BooleanLiteral(true))
} else if left.is_singleton(self.db)
&& left.is_equivalent_to(self.db, right)
{
Ok(Type::BooleanLiteral(false))
} else {
Ok(KnownClass::Bool.to_instance(self.db))
}
}
}
}
// TODO: handle more types