Skip to content

Commit

Permalink
red-knot: Add not unary operator for boolean literals (#13422)
Browse files Browse the repository at this point in the history
## Summary

Contributes to #12701

## Test Plan

Added test for boolean literals

Signed-off-by: haaris <haarisrahman@gmail.com>
  • Loading branch information
haarisr committed Sep 20, 2024
1 parent 7579a79 commit 6c303b2
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 6c303b2

Please sign in to comment.