Skip to content

Commit

Permalink
red-knot: Add not unary operator for integer literals
Browse files Browse the repository at this point in the history
  • Loading branch information
haarisr committed Sep 21, 2024
1 parent 6c303b2 commit 018c5a1
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2212,6 +2212,10 @@ 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),
(UnaryOp::Not, Type::IntLiteral(value)) => match value {
0 => Type::BooleanLiteral(true),
_ => Type::BooleanLiteral(false),
},
_ => Type::Unknown, // TODO other unary op types
}
}
Expand Down Expand Up @@ -3165,6 +3169,31 @@ mod tests {
Ok(())
}

#[test]
fn not_integer_literal() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_file(
"src/a.py",
r#"
a = not 1
b = not 1234567890987654321
e = not 0
x = not -1
y = not -1234567890987654321
z = not --987
"#,
)?;
assert_public_ty(&db, "src/a.py", "a", "Literal[False]");
assert_public_ty(&db, "src/a.py", "b", "Literal[False]");
assert_public_ty(&db, "src/a.py", "e", "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[False]");

Ok(())
}

#[test]
fn string_type() -> anyhow::Result<()> {
let mut db = setup_db();
Expand Down

0 comments on commit 018c5a1

Please sign in to comment.