From 6c303b24455f26ff08859affc080e43e1206ea6c Mon Sep 17 00:00:00 2001 From: haarisr <122410226+haarisr@users.noreply.github.com> Date: Fri, 20 Sep 2024 15:24:38 -0700 Subject: [PATCH] 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 --- .../src/types/infer.rs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index c41ec34a21996..de3384e4e54cd 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -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();