mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-29 16:03:50 +00:00
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:
parent
7579a792c7
commit
6c303b2445
1 changed files with 23 additions and 0 deletions
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue