Skip to content

Commit 6c303b2

Browse files
authored
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 <[email protected]>
1 parent 7579a79 commit 6c303b2

File tree

1 file changed

+23
-0
lines changed
  • crates/red_knot_python_semantic/src/types

1 file changed

+23
-0
lines changed

crates/red_knot_python_semantic/src/types/infer.rs

+23
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,7 @@ impl<'db> TypeInferenceBuilder<'db> {
22112211

22122212
match (op, self.infer_expression(operand)) {
22132213
(UnaryOp::USub, Type::IntLiteral(value)) => Type::IntLiteral(-value),
2214+
(UnaryOp::Not, Type::BooleanLiteral(value)) => Type::BooleanLiteral(!value),
22142215
_ => Type::Unknown, // TODO other unary op types
22152216
}
22162217
}
@@ -3142,6 +3143,28 @@ mod tests {
31423143
Ok(())
31433144
}
31443145

3146+
#[test]
3147+
fn not_boolean_literal() -> anyhow::Result<()> {
3148+
let mut db = setup_db();
3149+
3150+
db.write_file(
3151+
"src/a.py",
3152+
r#"
3153+
w = True
3154+
x = False
3155+
y = not w
3156+
z = not x
3157+
3158+
"#,
3159+
)?;
3160+
assert_public_ty(&db, "src/a.py", "w", "Literal[True]");
3161+
assert_public_ty(&db, "src/a.py", "x", "Literal[False]");
3162+
assert_public_ty(&db, "src/a.py", "y", "Literal[False]");
3163+
assert_public_ty(&db, "src/a.py", "z", "Literal[True]");
3164+
3165+
Ok(())
3166+
}
3167+
31453168
#[test]
31463169
fn string_type() -> anyhow::Result<()> {
31473170
let mut db = setup_db();

0 commit comments

Comments
 (0)