red-knot: Add not unary operator for boolean literals (#13422)

## Summary

Contributes to #12701

## Test Plan

Added test for boolean literals

Signed-off-by: haaris <haarisrahman@gmail.com>
This commit is contained in:
haarisr 2024-09-20 15:24:38 -07:00 committed by GitHub
parent 7579a792c7
commit 6c303b2445
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2211,6 +2211,7 @@ impl<'db> TypeInferenceBuilder<'db> {
match (op, self.infer_expression(operand)) {
(UnaryOp::USub, Type::IntLiteral(value)) => Type::IntLiteral(-value),
(UnaryOp::Not, Type::BooleanLiteral(value)) => Type::BooleanLiteral(!value),
_ => Type::Unknown, // TODO other unary op types
}
}
@ -3142,6 +3143,28 @@ mod tests {
Ok(())
}
#[test]
fn not_boolean_literal() -> anyhow::Result<()> {
let mut db = setup_db();
db.write_file(
"src/a.py",
r#"
w = True
x = False
y = not w
z = not x
"#,
)?;
assert_public_ty(&db, "src/a.py", "w", "Literal[True]");
assert_public_ty(&db, "src/a.py", "x", "Literal[False]");
assert_public_ty(&db, "src/a.py", "y", "Literal[False]");
assert_public_ty(&db, "src/a.py", "z", "Literal[True]");
Ok(())
}
#[test]
fn string_type() -> anyhow::Result<()> {
let mut db = setup_db();