Skip to content

Commit c16237d

Browse files
authored
[red-knot] Rework Type::to_instance() to return Option<Type> (#16428)
## Summary This PR fixes #16302. The PR reworks `Type::to_instance()` to return `Option<Type>` rather than `Type`. This reflects more accurately the fact that some variants cannot be "turned into an instance", since they _already_ represent instances of some kind. On `main`, we silently fallback to `Unknown` for these variants, but this implicit behaviour can be somewhat surprising and lead to unexpected bugs. Returning `Option<Type>` rather than `Type` means that each callsite has to account for the possibility that the type might already represent an instance, and decide what to do about it. In general, I think this increases the robustness of the code. Working on this PR revealed two latent bugs in the code: - One which has already been fixed by #16427 - One which is fixed as part of #16608 I added special handling to `KnownClass::to_instance()`: If we fail to find one of these classes and the `test` feature is _not_ enabled, we log a warning to the terminal saying that we failed to find the class in typeshed and that we will be falling back to `Type::Unknown`. A cache is maintained so that we record all classes that we have already logged a warning for; we only log a warning for failing to lookup a `KnownClass` if we know that it's the first time we're looking it up. ## Test Plan - All existing tests pass - I ran the property tests via `QUICKCHECK_TESTS=1000000 cargo test --release -p red_knot_python_semantic -- --ignored types::property_tests::stable` I also manually checked that warnings are appropriately printed to the terminal when `KnownClass::to_instance()` falls back to `Unknown` and the `test` feature is not enabled. To do this, I applied this diff to the PR branch: <details> <summary>Patch deleting `int` and `str` from buitins</summary> ```diff diff --git a/crates/red_knot_vendored/vendor/typeshed/stdlib/builtins.pyi b/crates/red_knot_vendored/vendor/typeshed/stdlib/builtins.pyi index 0a6dc57b0..86636a05b 100644 --- a/crates/red_knot_vendored/vendor/typeshed/stdlib/builtins.pyi +++ b/crates/red_knot_vendored/vendor/typeshed/stdlib/builtins.pyi @@ -228,111 +228,6 @@ _PositiveInteger: TypeAlias = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, _NegativeInteger: TypeAlias = Literal[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20] _LiteralInteger = _PositiveInteger | _NegativeInteger | Literal[0] # noqa: Y026 # TODO: Use TypeAlias once mypy bugs are fixed -class int: - @overload - def __new__(cls, x: ConvertibleToInt = ..., /) -> Self: ... - @overload - def __new__(cls, x: str | bytes | bytearray, /, base: SupportsIndex) -> Self: ... - def as_integer_ratio(self) -> tuple[int, Literal[1]]: ... - @Property - def real(self) -> int: ... - @Property - def imag(self) -> Literal[0]: ... - @Property - def numerator(self) -> int: ... - @Property - def denominator(self) -> Literal[1]: ... - def conjugate(self) -> int: ... - def bit_length(self) -> int: ... - if sys.version_info >= (3, 10): - def bit_count(self) -> int: ... - - if sys.version_info >= (3, 11): - def to_bytes( - self, length: SupportsIndex = 1, byteorder: Literal["little", "big"] = "big", *, signed: bool = False - ) -> bytes: ... - @classmethod - def from_bytes( - cls, - bytes: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer, - byteorder: Literal["little", "big"] = "big", - *, - signed: bool = False, - ) -> Self: ... - else: - def to_bytes(self, length: SupportsIndex, byteorder: Literal["little", "big"], *, signed: bool = False) -> bytes: ... - @classmethod - def from_bytes( - cls, - bytes: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer, - byteorder: Literal["little", "big"], - *, - signed: bool = False, - ) -> Self: ... - - if sys.version_info >= (3, 12): - def is_integer(self) -> Literal[True]: ... - - def __add__(self, value: int, /) -> int: ... - def __sub__(self, value: int, /) -> int: ... - def __mul__(self, value: int, /) -> int: ... - def __floordiv__(self, value: int, /) -> int: ... - def __truediv__(self, value: int, /) -> float: ... - def __mod__(self, value: int, /) -> int: ... - def __divmod__(self, value: int, /) -> tuple[int, int]: ... - def __radd__(self, value: int, /) -> int: ... - def __rsub__(self, value: int, /) -> int: ... - def __rmul__(self, value: int, /) -> int: ... - def __rfloordiv__(self, value: int, /) -> int: ... - def __rtruediv__(self, value: int, /) -> float: ... - def __rmod__(self, value: int, /) -> int: ... - def __rdivmod__(self, value: int, /) -> tuple[int, int]: ... - @overload - def __pow__(self, x: Literal[0], /) -> Literal[1]: ... - @overload - def __pow__(self, value: Literal[0], mod: None, /) -> Literal[1]: ... - @overload - def __pow__(self, value: _PositiveInteger, mod: None = None, /) -> int: ... - @overload - def __pow__(self, value: _NegativeInteger, mod: None = None, /) -> float: ... - # positive __value -> int; negative __value -> float - # return type must be Any as `int | float` causes too many false-positive errors - @overload - def __pow__(self, value: int, mod: None = None, /) -> Any: ... - @overload - def __pow__(self, value: int, mod: int, /) -> int: ... - def __rpow__(self, value: int, mod: int | None = None, /) -> Any: ... - def __and__(self, value: int, /) -> int: ... - def __or__(self, value: int, /) -> int: ... - def __xor__(self, value: int, /) -> int: ... - def __lshift__(self, value: int, /) -> int: ... - def __rshift__(self, value: int, /) -> int: ... - def __rand__(self, value: int, /) -> int: ... - def __ror__(self, value: int, /) -> int: ... - def __rxor__(self, value: int, /) -> int: ... - def __rlshift__(self, value: int, /) -> int: ... - def __rrshift__(self, value: int, /) -> int: ... - def __neg__(self) -> int: ... - def __pos__(self) -> int: ... - def __invert__(self) -> int: ... - def __trunc__(self) -> int: ... - def __ceil__(self) -> int: ... - def __floor__(self) -> int: ... - def __round__(self, ndigits: SupportsIndex = ..., /) -> int: ... - def __getnewargs__(self) -> tuple[int]: ... - def __eq__(self, value: object, /) -> bool: ... - def __ne__(self, value: object, /) -> bool: ... - def __lt__(self, value: int, /) -> bool: ... - def __le__(self, value: int, /) -> bool: ... - def __gt__(self, value: int, /) -> bool: ... - def __ge__(self, value: int, /) -> bool: ... - def __float__(self) -> float: ... - def __int__(self) -> int: ... - def __abs__(self) -> int: ... - def __hash__(self) -> int: ... - def __bool__(self) -> bool: ... - def __index__(self) -> int: ... - class float: def __new__(cls, x: ConvertibleToFloat = ..., /) -> Self: ... def as_integer_ratio(self) -> tuple[int, int]: ... @@ -437,190 +332,6 @@ class _FormatMapMapping(Protocol): class _TranslateTable(Protocol): def __getitem__(self, key: int, /) -> str | int | None: ... -class str(Sequence[str]): - @overload - def __new__(cls, object: object = ...) -> Self: ... - @overload - def __new__(cls, object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ... - @overload - def capitalize(self: LiteralString) -> LiteralString: ... - @overload - def capitalize(self) -> str: ... # type: ignore[misc] - @overload - def casefold(self: LiteralString) -> LiteralString: ... - @overload - def casefold(self) -> str: ... # type: ignore[misc] - @overload - def center(self: LiteralString, width: SupportsIndex, fillchar: LiteralString = " ", /) -> LiteralString: ... - @overload - def center(self, width: SupportsIndex, fillchar: str = " ", /) -> str: ... # type: ignore[misc] - def count(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ... - def encode(self, encoding: str = "utf-8", errors: str = "strict") -> bytes: ... - def endswith( - self, suffix: str | tuple[str, ...], start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., / - ) -> bool: ... - @overload - def expandtabs(self: LiteralString, tabsize: SupportsIndex = 8) -> LiteralString: ... - @overload - def expandtabs(self, tabsize: SupportsIndex = 8) -> str: ... # type: ignore[misc] - def find(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ... - @overload - def format(self: LiteralString, *args: LiteralString, **kwargs: LiteralString) -> LiteralString: ... - @overload - def format(self, *args: object, **kwargs: object) -> str: ... - def format_map(self, mapping: _FormatMapMapping, /) -> str: ... - def index(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ... - def isalnum(self) -> bool: ... - def isalpha(self) -> bool: ... - def isascii(self) -> bool: ... - def isdecimal(self) -> bool: ... - def isdigit(self) -> bool: ... - def isidentifier(self) -> bool: ... - def islower(self) -> bool: ... - def isnumeric(self) -> bool: ... - def isprintable(self) -> bool: ... - def isspace(self) -> bool: ... - def istitle(self) -> bool: ... - def isupper(self) -> bool: ... - @overload - def join(self: LiteralString, iterable: Iterable[LiteralString], /) -> LiteralString: ... - @overload - def join(self, iterable: Iterable[str], /) -> str: ... # type: ignore[misc] - @overload - def ljust(self: LiteralString, width: SupportsIndex, fillchar: LiteralString = " ", /) -> LiteralString: ... - @overload - def ljust(self, width: SupportsIndex, fillchar: str = " ", /) -> str: ... # type: ignore[misc] - @overload - def lower(self: LiteralString) -> LiteralString: ... - @overload - def lower(self) -> str: ... # type: ignore[misc] - @overload - def lstrip(self: LiteralString, chars: LiteralString | None = None, /) -> LiteralString: ... - @overload - def lstrip(self, chars: str | None = None, /) -> str: ... # type: ignore[misc] - @overload - def partition(self: LiteralString, sep: LiteralString, /) -> tuple[LiteralString, LiteralString, LiteralString]: ... - @overload - def partition(self, sep: str, /) -> tuple[str, str, str]: ... # type: ignore[misc] - if sys.version_info >= (3, 13): - @overload - def replace( - self: LiteralString, old: LiteralString, new: LiteralString, /, count: SupportsIndex = -1 - ) -> LiteralString: ... - @overload - def replace(self, old: str, new: str, /, count: SupportsIndex = -1) -> str: ... # type: ignore[misc] - else: - @overload - def replace( - self: LiteralString, old: LiteralString, new: LiteralString, count: SupportsIndex = -1, / - ) -> LiteralString: ... - @overload - def replace(self, old: str, new: str, count: SupportsIndex = -1, /) -> str: ... # type: ignore[misc] - if sys.version_info >= (3, 9): - @overload - def removeprefix(self: LiteralString, prefix: LiteralString, /) -> LiteralString: ... - @overload - def removeprefix(self, prefix: str, /) -> str: ... # type: ignore[misc] - @overload - def removesuffix(self: LiteralString, suffix: LiteralString, /) -> LiteralString: ... - @overload - def removesuffix(self, suffix: str, /) -> str: ... # type: ignore[misc] - - def rfind(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ... - def rindex(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ... - @overload - def rjust(self: LiteralString, width: SupportsIndex, fillchar: LiteralString = " ", /) -> LiteralString: ... - @overload - def rjust(self, width: SupportsIndex, fillchar: str = " ", /) -> str: ... # type: ignore[misc] - @overload - def rpartition(self: LiteralString, sep: LiteralString, /) -> tuple[LiteralString, LiteralString, LiteralString]: ... - @overload - def rpartition(self, sep: str, /) -> tuple[str, str, str]: ... # type: ignore[misc] - @overload - def rsplit(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ... - @overload - def rsplit(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc] - @overload - def rstrip(self: LiteralString, chars: LiteralString | None = None, /) -> LiteralString: ... - @overload - def rstrip(self, chars: str | None = None, /) -> str: ... # type: ignore[misc] - @overload - def split(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ... - @overload - def split(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc] - @overload - def splitlines(self: LiteralString, keepends: bool = False) -> list[LiteralString]: ... - @overload - def splitlines(self, keepends: bool = False) -> list[str]: ... # type: ignore[misc] - def startswith( - self, prefix: str | tuple[str, ...], start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., / - ) -> bool: ... - @overload - def strip(self: LiteralString, chars: LiteralString | None = None, /) -> LiteralString: ... - @overload - def strip(self, chars: str | None = None, /) -> str: ... # type: ignore[misc] - @overload - def swapcase(self: LiteralString) -> LiteralString: ... - @overload - def swapcase(self) -> str: ... # type: ignore[misc] - @overload - def title(self: LiteralString) -> LiteralString: ... - @overload - def title(self) -> str: ... # type: ignore[misc] - def translate(self, table: _TranslateTable, /) -> str: ... - @overload - def upper(self: LiteralString) -> LiteralString: ... - @overload - def upper(self) -> str: ... # type: ignore[misc] - @overload - def zfill(self: LiteralString, width: SupportsIndex, /) -> LiteralString: ... - @overload - def zfill(self, width: SupportsIndex, /) -> str: ... # type: ignore[misc] - @staticmethod - @overload - def maketrans(x: dict[int, _T] | dict[str, _T] | dict[str | int, _T], /) -> dict[int, _T]: ... - @staticmethod - @overload - def maketrans(x: str, y: str, /) -> dict[int, int]: ... - @staticmethod - @overload - def maketrans(x: str, y: str, z: str, /) -> dict[int, int | None]: ... - @overload - def __add__(self: LiteralString, value: LiteralString, /) -> LiteralString: ... - @overload - def __add__(self, value: str, /) -> str: ... # type: ignore[misc] - # Incompatible with Sequence.__contains__ - def __contains__(self, key: str, /) -> bool: ... # type: ignore[override] - def __eq__(self, value: object, /) -> bool: ... - def __ge__(self, value: str, /) -> bool: ... - @overload - def __getitem__(self: LiteralString, key: SupportsIndex | slice, /) -> LiteralString: ... - @overload - def __getitem__(self, key: SupportsIndex | slice, /) -> str: ... # type: ignore[misc] - def __gt__(self, value: str, /) -> bool: ... - def __hash__(self) -> int: ... - @overload - def __iter__(self: LiteralString) -> Iterator[LiteralString]: ... - @overload - def __iter__(self) -> Iterator[str]: ... # type: ignore[misc] - def __le__(self, value: str, /) -> bool: ... - def __len__(self) -> int: ... - def __lt__(self, value: str, /) -> bool: ... - @overload - def __mod__(self: LiteralString, value: LiteralString | tuple[LiteralString, ...], /) -> LiteralString: ... - @overload - def __mod__(self, value: Any, /) -> str: ... - @overload - def __mul__(self: LiteralString, value: SupportsIndex, /) -> LiteralString: ... - @overload - def __mul__(self, value: SupportsIndex, /) -> str: ... # type: ignore[misc] - def __ne__(self, value: object, /) -> bool: ... - @overload - def __rmul__(self: LiteralString, value: SupportsIndex, /) -> LiteralString: ... - @overload - def __rmul__(self, value: SupportsIndex, /) -> str: ... # type: ignore[misc] - def __getnewargs__(self) -> tuple[str]: ... - class bytes(Sequence[int]): ``` </details> And then ran red-knot on my [typeshed-stats](https://github.com/AlexWaygood/typeshed-stats) project using the command ``` cargo run -p red_knot -- check --project ../typeshed-stats --python-version="3.12" --verbose ``` I observed that the following logs were printed to the terminal, but that each warning was only printed once (the desired behaviour): ``` INFO Python version: Python 3.12, platform: all INFO Indexed 15 file(s) INFO Could not find class `builtins.int` in typeshed on Python 3.12. Falling back to `Unknown` for the symbol instead. INFO Could not find class `builtins.str` in typeshed on Python 3.12. Falling back to `Unknown` for the symbol instead. ```
1 parent 989075d commit c16237d

File tree

7 files changed

+245
-80
lines changed

7 files changed

+245
-80
lines changed

crates/red_knot_python_semantic/src/module_resolver/module.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ impl KnownModule {
134134
}
135135

136136
pub fn name(self) -> ModuleName {
137-
let self_as_str = self.as_str();
138-
ModuleName::new_static(self_as_str)
139-
.unwrap_or_else(|| panic!("{self_as_str} should be a valid module name!"))
137+
ModuleName::new_static(self.as_str())
138+
.unwrap_or_else(|| panic!("{self} should be a valid module name!"))
140139
}
141140

142141
pub(crate) fn try_from_search_path_and_name(
@@ -167,6 +166,12 @@ impl KnownModule {
167166
}
168167
}
169168

169+
impl std::fmt::Display for KnownModule {
170+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
171+
f.write_str(self.as_str())
172+
}
173+
}
174+
170175
#[cfg(test)]
171176
mod tests {
172177
use super::*;

crates/red_knot_python_semantic/src/types.rs

+34-43
Original file line numberDiff line numberDiff line change
@@ -730,10 +730,9 @@ impl<'db> Type<'db> {
730730
// `Literal[str]` is a subtype of `type` because the `str` class object is an instance of its metaclass `type`.
731731
// `Literal[abc.ABC]` is a subtype of `abc.ABCMeta` because the `abc.ABC` class object
732732
// is an instance of its metaclass `abc.ABCMeta`.
733-
(Type::ClassLiteral(ClassLiteralType { class }), _) => class
734-
.metaclass(db)
735-
.to_instance(db)
736-
.is_subtype_of(db, target),
733+
(Type::ClassLiteral(ClassLiteralType { class }), _) => {
734+
class.metaclass_instance_type(db).is_subtype_of(db, target)
735+
}
737736

738737
// `type[str]` (== `SubclassOf("str")` in red-knot) describes all possible runtime subclasses
739738
// of the class object `str`. It is a subtype of `type` (== `Instance("type")`) because `str`
@@ -745,11 +744,9 @@ impl<'db> Type<'db> {
745744
(Type::SubclassOf(subclass_of_ty), _) => subclass_of_ty
746745
.subclass_of()
747746
.into_class()
748-
.is_some_and(|class| {
749-
class
750-
.metaclass(db)
751-
.to_instance(db)
752-
.is_subtype_of(db, target)
747+
.map(|class| class.metaclass_instance_type(db))
748+
.is_some_and(|metaclass_instance_type| {
749+
metaclass_instance_type.is_subtype_of(db, target)
753750
}),
754751

755752
// For example: `Type::KnownInstance(KnownInstanceType::Type)` is a subtype of `Type::Instance(_SpecialForm)`,
@@ -1122,16 +1119,17 @@ impl<'db> Type<'db> {
11221119
ty.bool(db).is_always_true()
11231120
}
11241121

1122+
// for `type[Any]`/`type[Unknown]`/`type[Todo]`, we know the type cannot be any larger than `type`,
1123+
// so although the type is dynamic we can still determine disjointness in some situations
11251124
(Type::SubclassOf(subclass_of_ty), other)
1126-
| (other, Type::SubclassOf(subclass_of_ty)) => {
1127-
let metaclass_instance_ty = match subclass_of_ty.subclass_of() {
1128-
// for `type[Any]`/`type[Unknown]`/`type[Todo]`, we know the type cannot be any larger than `type`,
1129-
// so although the type is dynamic we can still determine disjointness in some situations
1130-
ClassBase::Dynamic(_) => KnownClass::Type.to_instance(db),
1131-
ClassBase::Class(class) => class.metaclass(db).to_instance(db),
1132-
};
1133-
other.is_disjoint_from(db, metaclass_instance_ty)
1134-
}
1125+
| (other, Type::SubclassOf(subclass_of_ty)) => match subclass_of_ty.subclass_of() {
1126+
ClassBase::Dynamic(_) => {
1127+
KnownClass::Type.to_instance(db).is_disjoint_from(db, other)
1128+
}
1129+
ClassBase::Class(class) => class
1130+
.metaclass_instance_type(db)
1131+
.is_disjoint_from(db, other),
1132+
},
11351133

11361134
(Type::KnownInstance(known_instance), Type::Instance(InstanceType { class }))
11371135
| (Type::Instance(InstanceType { class }), Type::KnownInstance(known_instance)) => {
@@ -1200,8 +1198,7 @@ impl<'db> Type<'db> {
12001198
(Type::ClassLiteral(ClassLiteralType { class }), instance @ Type::Instance(_))
12011199
| (instance @ Type::Instance(_), Type::ClassLiteral(ClassLiteralType { class })) => {
12021200
!class
1203-
.metaclass(db)
1204-
.to_instance(db)
1201+
.metaclass_instance_type(db)
12051202
.is_subtype_of(db, instance)
12061203
}
12071204

@@ -2106,19 +2103,13 @@ impl<'db> Type<'db> {
21062103
Type::FunctionLiteral(_) => Truthiness::AlwaysTrue,
21072104
Type::Callable(_) => Truthiness::AlwaysTrue,
21082105
Type::ModuleLiteral(_) => Truthiness::AlwaysTrue,
2109-
Type::ClassLiteral(ClassLiteralType { class }) => {
2110-
return class
2111-
.metaclass(db)
2112-
.to_instance(db)
2113-
.try_bool_impl(db, allow_short_circuit);
2114-
}
2106+
Type::ClassLiteral(ClassLiteralType { class }) => class
2107+
.metaclass_instance_type(db)
2108+
.try_bool_impl(db, allow_short_circuit)?,
21152109
Type::SubclassOf(subclass_of_ty) => match subclass_of_ty.subclass_of() {
21162110
ClassBase::Dynamic(_) => Truthiness::Ambiguous,
21172111
ClassBase::Class(class) => {
2118-
return class
2119-
.metaclass(db)
2120-
.to_instance(db)
2121-
.try_bool_impl(db, allow_short_circuit);
2112+
Type::class_literal(class).try_bool_impl(db, allow_short_circuit)?
21222113
}
21232114
},
21242115
Type::AlwaysTruthy => Truthiness::AlwaysTrue,
@@ -2948,19 +2939,19 @@ impl<'db> Type<'db> {
29482939
}
29492940

29502941
#[must_use]
2951-
pub fn to_instance(&self, db: &'db dyn Db) -> Type<'db> {
2942+
pub fn to_instance(&self, db: &'db dyn Db) -> Option<Type<'db>> {
29522943
match self {
2953-
Type::Dynamic(_) => *self,
2954-
Type::Never => Type::Never,
2955-
Type::ClassLiteral(ClassLiteralType { class }) => Type::instance(*class),
2956-
Type::SubclassOf(subclass_of_ty) => match subclass_of_ty.subclass_of() {
2957-
ClassBase::Class(class) => Type::instance(class),
2958-
ClassBase::Dynamic(dynamic) => Type::Dynamic(dynamic),
2959-
},
2960-
Type::Union(union) => union.map(db, |element| element.to_instance(db)),
2961-
Type::Intersection(_) => todo_type!("Type::Intersection.to_instance()"),
2962-
// TODO: calling `.to_instance()` on any of these should result in a diagnostic,
2963-
// since they already indicate that the object is an instance of some kind:
2944+
Type::Dynamic(_) | Type::Never => Some(*self),
2945+
Type::ClassLiteral(ClassLiteralType { class }) => Some(Type::instance(*class)),
2946+
Type::SubclassOf(subclass_of_ty) => Some(subclass_of_ty.to_instance()),
2947+
Type::Union(union) => {
2948+
let mut builder = UnionBuilder::new(db);
2949+
for element in union.elements(db) {
2950+
builder = builder.add(element.to_instance(db)?);
2951+
}
2952+
Some(builder.build())
2953+
}
2954+
Type::Intersection(_) => Some(todo_type!("Type::Intersection.to_instance()")),
29642955
Type::BooleanLiteral(_)
29652956
| Type::BytesLiteral(_)
29662957
| Type::FunctionLiteral(_)
@@ -2974,7 +2965,7 @@ impl<'db> Type<'db> {
29742965
| Type::Tuple(_)
29752966
| Type::LiteralString
29762967
| Type::AlwaysTruthy
2977-
| Type::AlwaysFalsy => Type::unknown(),
2968+
| Type::AlwaysFalsy => None,
29782969
}
29792970
}
29802971

crates/red_knot_python_semantic/src/types/class.rs

+160-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::{LazyLock, Mutex};
2+
13
use crate::{
24
module_resolver::file_to_module,
35
semantic_index::{
@@ -18,6 +20,7 @@ use indexmap::IndexSet;
1820
use itertools::Itertools as _;
1921
use ruff_db::files::File;
2022
use ruff_python_ast::{self as ast, PythonVersion};
23+
use rustc_hash::FxHashSet;
2124

2225
use super::{
2326
class_base::ClassBase, infer_expression_type, infer_unpack_types, IntersectionBuilder,
@@ -185,6 +188,14 @@ impl<'db> Class<'db> {
185188
.unwrap_or_else(|_| SubclassOfType::subclass_of_unknown())
186189
}
187190

191+
/// Return a type representing "the set of all instances of the metaclass of this class".
192+
pub(super) fn metaclass_instance_type(self, db: &'db dyn Db) -> Type<'db> {
193+
self
194+
.metaclass(db)
195+
.to_instance(db)
196+
.expect("`Type::to_instance()` should always return `Some()` when called on the type of a metaclass")
197+
}
198+
188199
/// Return the metaclass of this class, or an error if the metaclass cannot be inferred.
189200
#[salsa::tracked]
190201
pub(super) fn try_metaclass(self, db: &'db dyn Db) -> Result<Type<'db>, MetaclassError<'db>> {
@@ -879,7 +890,7 @@ impl<'db> KnownClass {
879890
}
880891
}
881892

882-
pub(crate) fn as_str(self, db: &'db dyn Db) -> &'static str {
893+
pub(crate) fn name(self, db: &'db dyn Db) -> &'static str {
883894
match self {
884895
Self::Bool => "bool",
885896
Self::Object => "object",
@@ -937,17 +948,101 @@ impl<'db> KnownClass {
937948
}
938949
}
939950

951+
fn display(self, db: &'db dyn Db) -> impl std::fmt::Display + 'db {
952+
struct KnownClassDisplay<'db> {
953+
db: &'db dyn Db,
954+
class: KnownClass,
955+
}
956+
957+
impl std::fmt::Display for KnownClassDisplay<'_> {
958+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
959+
let KnownClassDisplay {
960+
class: known_class,
961+
db,
962+
} = *self;
963+
write!(
964+
f,
965+
"{module}.{class}",
966+
module = known_class.canonical_module(db),
967+
class = known_class.name(db)
968+
)
969+
}
970+
}
971+
972+
KnownClassDisplay { db, class: self }
973+
}
974+
975+
/// Lookup a [`KnownClass`] in typeshed and return a [`Type`]
976+
/// representing all possible instances of the class.
977+
///
978+
/// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
940979
pub(crate) fn to_instance(self, db: &'db dyn Db) -> Type<'db> {
941-
self.to_class_literal(db).to_instance(db)
980+
self.to_class_literal(db)
981+
.into_class_literal()
982+
.map(|ClassLiteralType { class }| Type::instance(class))
983+
.unwrap_or_else(Type::unknown)
942984
}
943985

986+
/// Attempt to lookup a [`KnownClass`] in typeshed and return a [`Type`] representing that class-literal.
987+
///
988+
/// Return an error if the symbol cannot be found in the expected typeshed module,
989+
/// or if the symbol is not a class definition, or if the symbol is possibly unbound.
990+
pub(crate) fn try_to_class_literal(
991+
self,
992+
db: &'db dyn Db,
993+
) -> Result<ClassLiteralType<'db>, KnownClassLookupError<'db>> {
994+
let symbol = known_module_symbol(db, self.canonical_module(db), self.name(db)).symbol;
995+
match symbol {
996+
Symbol::Type(Type::ClassLiteral(class_type), Boundness::Bound) => Ok(class_type),
997+
Symbol::Type(Type::ClassLiteral(class_type), Boundness::PossiblyUnbound) => {
998+
Err(KnownClassLookupError::ClassPossiblyUnbound { class_type })
999+
}
1000+
Symbol::Type(found_type, _) => {
1001+
Err(KnownClassLookupError::SymbolNotAClass { found_type })
1002+
}
1003+
Symbol::Unbound => Err(KnownClassLookupError::ClassNotFound),
1004+
}
1005+
}
1006+
1007+
/// Lookup a [`KnownClass`] in typeshed and return a [`Type`] representing that class-literal.
1008+
///
1009+
/// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
9441010
pub(crate) fn to_class_literal(self, db: &'db dyn Db) -> Type<'db> {
945-
known_module_symbol(db, self.canonical_module(db), self.as_str(db))
946-
.symbol
947-
.ignore_possibly_unbound()
948-
.unwrap_or(Type::unknown())
1011+
// a cache of the `KnownClass`es that we have already failed to lookup in typeshed
1012+
// (and therefore that we've already logged a warning for)
1013+
static MESSAGES: LazyLock<Mutex<FxHashSet<KnownClass>>> = LazyLock::new(Mutex::default);
1014+
1015+
self.try_to_class_literal(db)
1016+
.map(Type::ClassLiteral)
1017+
.unwrap_or_else(|lookup_error| {
1018+
if MESSAGES.lock().unwrap().insert(self) {
1019+
if matches!(
1020+
lookup_error,
1021+
KnownClassLookupError::ClassPossiblyUnbound { .. }
1022+
) {
1023+
tracing::info!("{}", lookup_error.display(db, self));
1024+
} else {
1025+
tracing::info!(
1026+
"{}. Falling back to `Unknown` for the symbol instead.",
1027+
lookup_error.display(db, self)
1028+
);
1029+
}
1030+
}
1031+
1032+
match lookup_error {
1033+
KnownClassLookupError::ClassPossiblyUnbound { class_type, .. } => {
1034+
Type::class_literal(class_type.class)
1035+
}
1036+
KnownClassLookupError::ClassNotFound { .. }
1037+
| KnownClassLookupError::SymbolNotAClass { .. } => Type::unknown(),
1038+
}
1039+
})
9491040
}
9501041

1042+
/// Lookup a [`KnownClass`] in typeshed and return a [`Type`]
1043+
/// representing that class and all possible subclasses of the class.
1044+
///
1045+
/// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
9511046
pub(crate) fn to_subclass_of(self, db: &'db dyn Db) -> Type<'db> {
9521047
self.to_class_literal(db)
9531048
.into_class_literal()
@@ -958,11 +1053,8 @@ impl<'db> KnownClass {
9581053
/// Return `true` if this symbol can be resolved to a class definition `class` in typeshed,
9591054
/// *and* `class` is a subclass of `other`.
9601055
pub(super) fn is_subclass_of(self, db: &'db dyn Db, other: Class<'db>) -> bool {
961-
known_module_symbol(db, self.canonical_module(db), self.as_str(db))
962-
.symbol
963-
.ignore_possibly_unbound()
964-
.and_then(Type::into_class_literal)
965-
.is_some_and(|ClassLiteralType { class }| class.is_subclass_of(db, other))
1056+
self.try_to_class_literal(db)
1057+
.is_ok_and(|ClassLiteralType { class }| class.is_subclass_of(db, other))
9661058
}
9671059

9681060
/// Return the module in which we should look up the definition for this class
@@ -1227,6 +1319,62 @@ impl<'db> KnownClass {
12271319
}
12281320
}
12291321

1322+
/// Enumeration of ways in which looking up a [`KnownClass`] in typeshed could fail.
1323+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1324+
pub(crate) enum KnownClassLookupError<'db> {
1325+
/// There is no symbol by that name in the expected typeshed module.
1326+
ClassNotFound,
1327+
/// There is a symbol by that name in the expected typeshed module,
1328+
/// but it's not a class.
1329+
SymbolNotAClass { found_type: Type<'db> },
1330+
/// There is a symbol by that name in the expected typeshed module,
1331+
/// and it's a class definition, but it's possibly unbound.
1332+
ClassPossiblyUnbound { class_type: ClassLiteralType<'db> },
1333+
}
1334+
1335+
impl<'db> KnownClassLookupError<'db> {
1336+
fn display(&self, db: &'db dyn Db, class: KnownClass) -> impl std::fmt::Display + 'db {
1337+
struct ErrorDisplay<'db> {
1338+
db: &'db dyn Db,
1339+
class: KnownClass,
1340+
error: KnownClassLookupError<'db>,
1341+
}
1342+
1343+
impl std::fmt::Display for ErrorDisplay<'_> {
1344+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1345+
let ErrorDisplay { db, class, error } = *self;
1346+
1347+
let class = class.display(db);
1348+
let python_version = Program::get(db).python_version(db);
1349+
1350+
match error {
1351+
KnownClassLookupError::ClassNotFound => write!(
1352+
f,
1353+
"Could not find class `{class}` in typeshed on Python {python_version}",
1354+
),
1355+
KnownClassLookupError::SymbolNotAClass { found_type } => write!(
1356+
f,
1357+
"Error looking up `{class}` in typeshed: expected to find a class definition \
1358+
on Python {python_version}, but found a symbol of type `{found_type}` instead",
1359+
found_type = found_type.display(db),
1360+
),
1361+
KnownClassLookupError::ClassPossiblyUnbound { .. } => write!(
1362+
f,
1363+
"Error looking up `{class}` in typeshed on Python {python_version}: \
1364+
expected to find a fully bound symbol, but found one that is possibly unbound",
1365+
)
1366+
}
1367+
}
1368+
}
1369+
1370+
ErrorDisplay {
1371+
db,
1372+
class,
1373+
error: *self,
1374+
}
1375+
}
1376+
}
1377+
12301378
/// Enumeration of specific runtime that are special enough to be considered their own type.
12311379
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, salsa::Update)]
12321380
pub enum KnownInstanceType<'db> {
@@ -1609,7 +1757,7 @@ mod tests {
16091757
fn known_class_roundtrip_from_str() {
16101758
let db = setup_db();
16111759
for class in KnownClass::iter() {
1612-
let class_name = class.as_str(&db);
1760+
let class_name = class.name(&db);
16131761
let class_module = resolve_module(&db, &class.canonical_module(&db).name()).unwrap();
16141762

16151763
assert_eq!(

0 commit comments

Comments
 (0)