Skip to content

Commit 2624249

Browse files
authored
[red-knot] Minor: fix Literal[True] <: int (#14177)
## Summary Minor fix to `Type::is_subtype_of` to make sure that Boolean literals are subtypes of `int`, to match runtime semantics. Found this while doing some property-testing experiments [1]. [1] #14178 ## Test Plan New unit test.
1 parent 4b08d17 commit 2624249

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

  • crates/red_knot_python_semantic/src

crates/red_knot_python_semantic/src/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl<'db> Type<'db> {
503503
false
504504
}
505505
(Type::BooleanLiteral(_), Type::Instance(InstanceType { class }))
506-
if class.is_known(db, KnownClass::Bool) =>
506+
if matches!(class.known(db), Some(KnownClass::Bool | KnownClass::Int)) =>
507507
{
508508
true
509509
}
@@ -2789,6 +2789,7 @@ mod tests {
27892789
#[test_case(Ty::IntLiteral(1), Ty::BuiltinInstance("int"))]
27902790
#[test_case(Ty::IntLiteral(1), Ty::BuiltinInstance("object"))]
27912791
#[test_case(Ty::BooleanLiteral(true), Ty::BuiltinInstance("bool"))]
2792+
#[test_case(Ty::BooleanLiteral(true), Ty::BuiltinInstance("int"))]
27922793
#[test_case(Ty::BooleanLiteral(true), Ty::BuiltinInstance("object"))]
27932794
#[test_case(Ty::StringLiteral("foo"), Ty::BuiltinInstance("str"))]
27942795
#[test_case(Ty::StringLiteral("foo"), Ty::BuiltinInstance("object"))]

0 commit comments

Comments
 (0)