Skip to content

Commit b9066cf

Browse files
committed
further improvements to comments
1 parent 638885e commit b9066cf

File tree

1 file changed

+13
-7
lines changed
  • crates/red_knot_python_semantic/src

1 file changed

+13
-7
lines changed

crates/red_knot_python_semantic/src/types.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,11 @@ impl<'db> Type<'db> {
792792
}),
793793
) => self_class.is_subclass_of(db, target_class),
794794

795-
// `Literal[str]` is a subtype of `type` because `str` is an instance of `type`.
796-
// `Literal[enum.Enum]` is a subtype of `enum.EnumMeta` because `enum.Enum`
795+
// `type[str]` (== `SubclassOf("str")` in red-knot) describes all possible runtime subclasses
796+
// of the class object `str`. It is a subtype of `type` (== `Instance("type")`) because `str`
797+
// is an instance of `type`, and so all possible subclasses of `str` will also be instances of `type`.
798+
//
799+
// Similarly `type[enum.Enum]` is a subtype of `enum.EnumMeta` because `enum.Enum`
797800
// is an instance of `enum.EnumMeta`.
798801
(
799802
Type::SubclassOf(SubclassOfType {
@@ -808,14 +811,16 @@ impl<'db> Type<'db> {
808811
(Type::SubclassOf(_), _) => KnownClass::Type.to_instance(db).is_subtype_of(db, target),
809812

810813
// For example: `Type::KnownInstance(KnownInstanceType::Type)` is a subtype of `Type::Instance(_SpecialForm)`,
811-
// because the symbol `typing.Type` is an instance of `typing._SpecialForm` at runtime
814+
// because `Type::KnownInstance(KnownInstanceType::Type)` is a set with exactly one runtime value in it
815+
// (the symbol `typing.Type`), and that symbol is known to be an instance of `typing._SpecialForm` at runtime.
812816
(Type::KnownInstance(left), right) => {
813817
left.instance_fallback(db).is_subtype_of(db, right)
814818
}
815819

816-
// For example, `Instance(ABCMeta)` is a subtype of `type[object]`,
817-
// because (since `ABCMeta` subclasses `type`) all instances of `ABCMeta` are instances of `type`,
818-
// and all instances of `type` are members of the type `type[object]`
820+
// For example, `abc.ABCMeta` (== `Instance("abc.ABCMeta")`) is a subtype of `type[object]`
821+
// (== `SubclassOf("object")`) because (since `abc.ABCMeta` subclasses `type`) all instances of `ABCMeta`
822+
// are instances of `type`, and `type[object]` represents the set of all subclasses of `object`,
823+
// which is exactly equal to the set of all instances of `type`.
819824
(
820825
Type::Instance(_),
821826
Type::SubclassOf(SubclassOfType {
@@ -825,7 +830,8 @@ impl<'db> Type<'db> {
825830
self.is_subtype_of(db, KnownClass::Type.to_instance(db))
826831
}
827832

828-
// `bool` is a subtype of `int`, because all instances of `bool` are also instances of `int`.
833+
// `bool` is a subtype of `int`, because `bool` subclasses `int`,
834+
// which means that all instances of `bool` are also instances of `int`
829835
(Type::Instance(self_instance), Type::Instance(target_instance)) => {
830836
self_instance.is_subtype_of(db, target_instance)
831837
}

0 commit comments

Comments
 (0)