From b4538f13cb96daff384e535601c5d0e4fdca4ec4 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 30 Sep 2024 13:50:58 +0100 Subject: [PATCH 1/2] [red-knot] Allow calling `bool()` with no arguments --- crates/red_knot_python_semantic/src/types.rs | 10 +++++----- crates/red_knot_python_semantic/src/types/infer.rs | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index 21278a423291ae..b0f28772f282a0 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -592,11 +592,11 @@ impl<'db> Type<'db> { // the specific truthiness value of the input arg, `Literal[True]` for the example above. let is_bool = class.is_stdlib_symbol(db, "builtins", "bool"); CallOutcome::callable(if is_bool { - arg_types - .first() - .unwrap_or(&Type::Unknown) - .bool(db) - .into_type(db) + if let Some(arg) = arg_types.first() { + arg.bool(db).into_type(db) + } else { + Type::BooleanLiteral(false) + } } else { Type::Instance(class) }) diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index b2a0c3d768dd98..1894283c09d431 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -6494,6 +6494,7 @@ mod tests { c = bool(None) d = bool("") e = bool(False) + f = bool() "#, )?; assert_public_ty(&db, "/src/a.py", "a", "Literal[False]"); @@ -6501,6 +6502,7 @@ mod tests { assert_public_ty(&db, "/src/a.py", "c", "Literal[False]"); assert_public_ty(&db, "/src/a.py", "d", "Literal[False]"); assert_public_ty(&db, "/src/a.py", "e", "Literal[False]"); + assert_public_ty(&db, "/src/a.py", "f", "Literal[False]"); Ok(()) } From 25a2de0de5c363c16aa32e70bce3c0794971fc44 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 30 Sep 2024 14:13:35 +0100 Subject: [PATCH 2/2] use `map()` --- crates/red_knot_python_semantic/src/types.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index b0f28772f282a0..7baf170c914138 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -592,11 +592,10 @@ impl<'db> Type<'db> { // the specific truthiness value of the input arg, `Literal[True]` for the example above. let is_bool = class.is_stdlib_symbol(db, "builtins", "bool"); CallOutcome::callable(if is_bool { - if let Some(arg) = arg_types.first() { - arg.bool(db).into_type(db) - } else { - Type::BooleanLiteral(false) - } + arg_types + .first() + .map(|arg| arg.bool(db).into_type(db)) + .unwrap_or(Type::BooleanLiteral(false)) } else { Type::Instance(class) })