Skip to content

Commit 638885e

Browse files
committed
readability improvements
1 parent 1371962 commit 638885e

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

crates/red_knot_python_semantic/src/types.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

30633064
impl<'db> From<ClassLiteralType<'db>> for Type<'db> {
@@ -3085,9 +3086,8 @@ pub struct InstanceType<'db> {
30853086
}
30863087

30873088
impl<'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

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3368,8 +3368,8 @@ impl<'db> TypeInferenceBuilder<'db> {
33683368
op,
33693369
),
33703370

3371-
(left_ty @ Type::Instance(left), right_ty @ Type::Instance(right), op) => {
3372-
if left != right && right.is_instance_of(self.db, left.class) {
3371+
(Type::Instance(left), Type::Instance(right), op) => {
3372+
if left != right && right.is_subtype_of(self.db, left) {
33733373
let reflected_dunder = op.reflected_dunder();
33743374
let rhs_reflected = right.class.class_member(self.db, reflected_dunder);
33753375
if !rhs_reflected.is_unbound()
@@ -5269,7 +5269,7 @@ fn perform_rich_comparison<'db>(
52695269
};
52705270

52715271
// The reflected dunder has priority if the right-hand side is a strict subclass of the left-hand side.
5272-
if left != right && right.is_instance_of(db, left.class) {
5272+
if left != right && right.is_subtype_of(db, left) {
52735273
call_dunder(op.reflect(), right, left).or_else(|| call_dunder(op, left, right))
52745274
} else {
52755275
call_dunder(op, left, right).or_else(|| call_dunder(op.reflect(), right, left))

0 commit comments

Comments
 (0)