Skip to content

Commit

Permalink
[red-knot] Minor: fix Literal[True] <: int (#14177)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
sharkdp authored Nov 7, 2024
1 parent 4b08d17 commit 2624249
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ impl<'db> Type<'db> {
false
}
(Type::BooleanLiteral(_), Type::Instance(InstanceType { class }))
if class.is_known(db, KnownClass::Bool) =>
if matches!(class.known(db), Some(KnownClass::Bool | KnownClass::Int)) =>
{
true
}
Expand Down Expand Up @@ -2789,6 +2789,7 @@ mod tests {
#[test_case(Ty::IntLiteral(1), Ty::BuiltinInstance("int"))]
#[test_case(Ty::IntLiteral(1), Ty::BuiltinInstance("object"))]
#[test_case(Ty::BooleanLiteral(true), Ty::BuiltinInstance("bool"))]
#[test_case(Ty::BooleanLiteral(true), Ty::BuiltinInstance("int"))]
#[test_case(Ty::BooleanLiteral(true), Ty::BuiltinInstance("object"))]
#[test_case(Ty::StringLiteral("foo"), Ty::BuiltinInstance("str"))]
#[test_case(Ty::StringLiteral("foo"), Ty::BuiltinInstance("object"))]
Expand Down

0 comments on commit 2624249

Please sign in to comment.