@@ -679,7 +679,7 @@ impl<'db> Type<'db> {
679679 target_neg_elem. is_subtype_of ( db, self_neg_elem)
680680 // Is target negative value is disjoint from a self positive value?
681681 } ) || self_intersection. positive ( db) . iter ( ) . any ( |& self_pos_elem| {
682- target_neg_elem . is_disjoint_from ( db, self_pos_elem )
682+ self_pos_elem . is_disjoint_from ( db, target_neg_elem )
683683 } )
684684 } )
685685 }
@@ -695,7 +695,7 @@ impl<'db> Type<'db> {
695695 && intersection
696696 . negative ( db)
697697 . iter ( )
698- . all ( |& neg_ty| neg_ty . is_disjoint_from ( db, self ) )
698+ . all ( |& neg_ty| self . is_disjoint_from ( db, neg_ty ) )
699699 }
700700
701701 // All `StringLiteral` types are a subtype of `LiteralString`.
@@ -802,7 +802,7 @@ impl<'db> Type<'db> {
802802 Type :: Instance ( InstanceType {
803803 class : target_class,
804804 } ) ,
805- ) => ClassLiteralType { class : self_class } . is_instance_of ( db, target_class) ,
805+ ) => self_class. is_instance_of ( db, target_class) ,
806806
807807 // Other than the cases enumerated above, `type[]` just delegates to `Instance("type")`
808808 ( Type :: SubclassOf ( _) , _) => KnownClass :: Type . to_instance ( db) . is_subtype_of ( db, target) ,
@@ -826,7 +826,9 @@ impl<'db> Type<'db> {
826826 }
827827
828828 // `bool` is a subtype of `int`, because all instances of `bool` are also instances of `int`.
829- ( Type :: Instance ( left) , Type :: Instance ( right) ) => left. is_instance_of ( db, right. class ) ,
829+ ( Type :: Instance ( self_instance) , Type :: Instance ( target_instance) ) => {
830+ self_instance. is_subtype_of ( db, target_instance)
831+ }
830832
831833 // Other than the special cases enumerated above,
832834 // `Instance` types are never subtypes of any other variants
@@ -2842,6 +2844,17 @@ impl<'db> Class<'db> {
28422844 self . iter_mro ( db) . contains ( & ClassBase :: Class ( other) )
28432845 }
28442846
2847+ /// Return `true` if this class object is an instance of the class `other`.
2848+ ///
2849+ /// A class is an instance of its metaclass; consequently,
2850+ /// a class will only ever be an instance of another class
2851+ /// if its metaclass is a subclass of that other class.
2852+ fn is_instance_of ( self , db : & ' db dyn Db , other : Class < ' db > ) -> bool {
2853+ self . metaclass ( db) . into_class_literal ( ) . is_some_and (
2854+ |ClassLiteralType { class : metaclass } | metaclass. is_subclass_of ( db, other) ,
2855+ )
2856+ }
2857+
28452858 /// Return the explicit `metaclass` of this class, if one is defined.
28462859 ///
28472860 /// ## Note
@@ -3046,18 +3059,6 @@ impl<'db> ClassLiteralType<'db> {
30463059 fn member ( self , db : & ' db dyn Db , name : & str ) -> Symbol < ' db > {
30473060 self . class . class_member ( db, name)
30483061 }
3049-
3050- /// Return `true` if the singleton class object represented by this type
3051- /// is an instance of the class `other`.
3052- ///
3053- /// A class is an instance of its metaclass; consequently,
3054- /// a class will only ever be an instance of another class
3055- /// if its metaclass is a subclass of that other class.
3056- fn is_instance_of ( self , db : & ' db dyn Db , other : Class < ' db > ) -> bool {
3057- self . class . metaclass ( db) . into_class_literal ( ) . is_some_and (
3058- |ClassLiteralType { class : metaclass } | metaclass. is_subclass_of ( db, other) ,
3059- )
3060- }
30613062}
30623063
30633064impl < ' db > From < ClassLiteralType < ' db > > for Type < ' db > {
@@ -3085,9 +3086,8 @@ pub struct InstanceType<'db> {
30853086}
30863087
30873088impl < ' db > InstanceType < ' db > {
3088- /// Return `true` if members of this type are instances of the class `class` at runtime.
3089- pub fn is_instance_of ( self , db : & ' db dyn Db , class : Class < ' db > ) -> bool {
3090- self . class . is_subclass_of ( db, class)
3089+ fn is_subtype_of ( self , db : & ' db dyn Db , other : InstanceType < ' db > ) -> bool {
3090+ self . class . is_subclass_of ( db, other. class )
30913091 }
30923092}
30933093
0 commit comments