From 37239dc0825ae7d16b91d64c8ca31c98b0cb5c0d Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 14 Dec 2024 15:35:14 +0000 Subject: [PATCH 1/2] Add failing property tests --- crates/red_knot_python_semantic/src/types.rs | 7 +++++++ .../red_knot_python_semantic/src/types/property_tests.rs | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index a838a4abad859..ae395c7da78a7 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -3352,6 +3352,7 @@ pub(crate) mod tests { Tuple(Vec), SubclassOfAny, SubclassOfBuiltinClass(&'static str), + SubclassOfAbcClass(&'static str), StdlibModule(CoreStdlibModule), SliceLiteral(i32, i32, i32), } @@ -3404,6 +3405,12 @@ pub(crate) mod tests { .expect_class_literal() .class, ), + Ty::SubclassOfAbcClass(s) => Type::subclass_of( + core_module_symbol(db, CoreStdlibModule::Abc, s) + .expect_type() + .expect_class_literal() + .class, + ), Ty::StdlibModule(module) => { Type::ModuleLiteral(resolve_module(db, &module.name()).unwrap().file()) } diff --git a/crates/red_knot_python_semantic/src/types/property_tests.rs b/crates/red_knot_python_semantic/src/types/property_tests.rs index 381bffa7afde4..83f49ea22039a 100644 --- a/crates/red_knot_python_semantic/src/types/property_tests.rs +++ b/crates/red_knot_python_semantic/src/types/property_tests.rs @@ -65,9 +65,16 @@ fn arbitrary_core_type(g: &mut Gen) -> Ty { Ty::BuiltinClassLiteral("bool"), Ty::BuiltinClassLiteral("object"), Ty::BuiltinInstance("type"), + Ty::AbcInstance("ABC"), + Ty::AbcInstance("ABCMeta"), Ty::SubclassOfAny, Ty::SubclassOfBuiltinClass("object"), Ty::SubclassOfBuiltinClass("str"), + Ty::SubclassOfBuiltinClass("type"), + Ty::AbcClassLiteral("ABC"), + Ty::AbcClassLiteral("ABCMeta"), + Ty::SubclassOfAbcClass("ABC"), + Ty::SubclassOfAbcClass("ABCMeta"), ]) .unwrap() .clone() From 8d4fbf957df9a223c1b0926a7bc38bc21f5d7664 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 14 Dec 2024 15:39:26 +0000 Subject: [PATCH 2/2] a class-literal type is not disjoint from an instance type referring to the class's metaclass --- crates/red_knot_python_semantic/src/types.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index ae395c7da78a7..b520549c72461 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -1157,10 +1157,15 @@ impl<'db> Type<'db> { Some(KnownClass::Slice | KnownClass::Object) ), - (Type::ClassLiteral(..), Type::Instance(InstanceType { class })) - | (Type::Instance(InstanceType { class }), Type::ClassLiteral(..)) => { - !matches!(class.known(db), Some(KnownClass::Type | KnownClass::Object)) - } + ( + Type::ClassLiteral(ClassLiteralType { class: class_a }), + Type::Instance(InstanceType { class: class_b }), + ) + | ( + Type::Instance(InstanceType { class: class_b }), + Type::ClassLiteral(ClassLiteralType { class: class_a }), + ) => !class_a.is_instance_of(db, class_b), + (Type::FunctionLiteral(..), Type::Instance(InstanceType { class })) | (Type::Instance(InstanceType { class }), Type::FunctionLiteral(..)) => !matches!( class.known(db), @@ -3751,6 +3756,7 @@ pub(crate) mod tests { #[test_case(Ty::Tuple(vec![Ty::IntLiteral(1), Ty::IntLiteral(2)]), Ty::Tuple(vec![Ty::IntLiteral(1), Ty::BuiltinInstance("int")]))] #[test_case(Ty::BuiltinClassLiteral("str"), Ty::BuiltinInstance("type"))] #[test_case(Ty::BuiltinClassLiteral("str"), Ty::SubclassOfAny)] + #[test_case(Ty::AbcClassLiteral("ABC"), Ty::AbcInstance("ABCMeta"))] fn is_not_disjoint_from(a: Ty, b: Ty) { let db = setup_db(); let a = a.into_type(&db);