Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/red_knot_python_semantic/src/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub(crate) enum CoreStdlibModule {
TypingExtensions,
Typing,
Sys,
#[allow(dead_code)]
Abc, // currently only used in tests
}

impl CoreStdlibModule {
Expand All @@ -26,6 +28,7 @@ impl CoreStdlibModule {
Self::Typeshed => "_typeshed",
Self::TypingExtensions => "typing_extensions",
Self::Sys => "sys",
Self::Abc => "abc",
}
}

Expand Down
62 changes: 45 additions & 17 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,19 @@ impl<'db> Type<'db> {
},
)
}
(Type::ClassLiteral(self_class), Type::SubclassOf(target_class)) => {
self_class.class.is_subclass_of_base(db, target_class.base)
}
(
Type::Instance(InstanceType { class: self_class }),
Type::SubclassOf(target_class),
) if self_class.is_known(db, KnownClass::Type) => {
self_class.is_subclass_of_base(db, target_class.base)
Type::ClassLiteral(ClassLiteralType { class: self_class }),
Type::SubclassOf(SubclassOfType {
base: ClassBase::Class(target_class),
}),
) => self_class.is_subclass_of(db, target_class),
(
Type::Instance(_),
Type::SubclassOf(SubclassOfType {
base: ClassBase::Class(target_class),
}),
) if target_class.is_known(db, KnownClass::Object) => {
self.is_subtype_of(db, KnownClass::Type.to_instance(db))
}
(
Type::SubclassOf(SubclassOfType {
Expand Down Expand Up @@ -928,10 +933,18 @@ impl<'db> Type<'db> {
| Type::ClassLiteral(..)),
) => left != right,

(Type::SubclassOf(type_class), Type::ClassLiteral(class_literal))
| (Type::ClassLiteral(class_literal), Type::SubclassOf(type_class)) => {
!class_literal.class.is_subclass_of_base(db, type_class.base)
}
(
Type::SubclassOf(SubclassOfType {
base: ClassBase::Class(class_a),
}),
Type::ClassLiteral(ClassLiteralType { class: class_b }),
)
| (
Type::ClassLiteral(ClassLiteralType { class: class_b }),
Type::SubclassOf(SubclassOfType {
base: ClassBase::Class(class_a),
}),
) => !class_b.is_subclass_of(db, class_a),
(Type::SubclassOf(_), Type::SubclassOf(_)) => false,
(Type::SubclassOf(_), Type::Instance(_)) | (Type::Instance(_), Type::SubclassOf(_)) => {
false
Expand Down Expand Up @@ -2593,11 +2606,7 @@ impl<'db> Class<'db> {
pub fn is_subclass_of(self, db: &'db dyn Db, other: Class) -> bool {
// `is_subclass_of` is checking the subtype relation, in which gradual types do not
// participate, so we should not return `True` if we find `Any/Unknown` in the MRO.
self.is_subclass_of_base(db, other)
}

fn is_subclass_of_base(self, db: &'db dyn Db, other: impl Into<ClassBase<'db>>) -> bool {
self.iter_mro(db).contains(&other.into())
self.iter_mro(db).contains(&ClassBase::Class(other))
}

/// Return the explicit `metaclass` of this class, if one is defined.
Expand Down Expand Up @@ -3036,12 +3045,18 @@ pub(crate) mod tests {
// BuiltinInstance("str") corresponds to an instance of the builtin `str` class
BuiltinInstance(&'static str),
TypingInstance(&'static str),
/// Members of the `abc` stdlib module
AbcInstance(&'static str),
AbcClassLiteral(&'static str),
TypingLiteral,
// BuiltinClassLiteral("str") corresponds to the builtin `str` class object itself
BuiltinClassLiteral(&'static str),
KnownClassInstance(KnownClass),
Union(Vec<Ty>),
Intersection { pos: Vec<Ty>, neg: Vec<Ty> },
Intersection {
pos: Vec<Ty>,
neg: Vec<Ty>,
},
Comment on lines +3056 to +3059
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit, but it doesn't seem necessary to explode this onto multiple lines? Is rustfmt doing that because the trailing comma was added?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea why rustfmt is doing it! I can see if I can get rustfmt to undo it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot get rustfmt to undo it. Ours not to reason why?

Tuple(Vec<Ty>),
SubclassOfAny,
SubclassOfBuiltinClass(&'static str),
Expand All @@ -3061,6 +3076,12 @@ pub(crate) mod tests {
Ty::LiteralString => Type::LiteralString,
Ty::BytesLiteral(s) => Type::bytes_literal(db, s.as_bytes()),
Ty::BuiltinInstance(s) => builtins_symbol(db, s).expect_type().to_instance(db),
Ty::AbcInstance(s) => core_module_symbol(db, CoreStdlibModule::Abc, s)
.expect_type()
.to_instance(db),
Ty::AbcClassLiteral(s) => {
core_module_symbol(db, CoreStdlibModule::Abc, s).expect_type()
}
Ty::TypingInstance(s) => typing_symbol(db, s).expect_type().to_instance(db),
Ty::TypingLiteral => Type::KnownInstance(KnownInstanceType::Literal),
Ty::BuiltinClassLiteral(s) => builtins_symbol(db, s).expect_type(),
Expand Down Expand Up @@ -3146,6 +3167,7 @@ pub(crate) mod tests {
#[test_case(Ty::BuiltinInstance("type"), Ty::SubclassOfAny)]
#[test_case(Ty::BuiltinInstance("type"), Ty::SubclassOfBuiltinClass("object"))]
#[test_case(Ty::BuiltinInstance("type"), Ty::BuiltinInstance("type"))]
#[test_case(Ty::BuiltinClassLiteral("str"), Ty::SubclassOfAny)]
fn is_assignable_to(from: Ty, to: Ty) {
let db = setup_db();
assert!(from.into_type(&db).is_assignable_to(&db, to.into_type(&db)));
Expand Down Expand Up @@ -3212,6 +3234,8 @@ pub(crate) mod tests {
#[test_case(Ty::BuiltinClassLiteral("int"), Ty::BuiltinInstance("object"))]
#[test_case(Ty::TypingLiteral, Ty::TypingInstance("_SpecialForm"))]
#[test_case(Ty::TypingLiteral, Ty::BuiltinInstance("object"))]
#[test_case(Ty::AbcClassLiteral("ABC"), Ty::AbcInstance("ABCMeta"))]
#[test_case(Ty::AbcInstance("ABCMeta"), Ty::SubclassOfBuiltinClass("object"))]
fn is_subtype_of(from: Ty, to: Ty) {
let db = setup_db();
assert!(from.into_type(&db).is_subtype_of(&db, to.into_type(&db)));
Expand Down Expand Up @@ -3242,6 +3266,9 @@ pub(crate) mod tests {
#[test_case(Ty::BuiltinClassLiteral("int"), Ty::BuiltinClassLiteral("object"))]
#[test_case(Ty::BuiltinInstance("int"), Ty::BuiltinClassLiteral("int"))]
#[test_case(Ty::TypingInstance("_SpecialForm"), Ty::TypingLiteral)]
#[test_case(Ty::BuiltinInstance("type"), Ty::SubclassOfBuiltinClass("str"))]
#[test_case(Ty::BuiltinClassLiteral("str"), Ty::SubclassOfAny)]
#[test_case(Ty::AbcInstance("ABCMeta"), Ty::SubclassOfBuiltinClass("type"))]
fn is_not_subtype_of(from: Ty, to: Ty) {
let db = setup_db();
assert!(!from.into_type(&db).is_subtype_of(&db, to.into_type(&db)));
Expand Down Expand Up @@ -3401,6 +3428,7 @@ pub(crate) mod tests {
#[test_case(Ty::Intersection{pos: vec![Ty::BuiltinInstance("int"), Ty::IntLiteral(2)], neg: vec![]}, Ty::IntLiteral(2))]
#[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)]
fn is_not_disjoint_from(a: Ty, b: Ty) {
let db = setup_db();
let a = a.into_type(&db);
Expand Down
Loading