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
28 changes: 14 additions & 14 deletions crates/ty_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,17 +812,17 @@ impl<'db> Type<'db> {
}

fn is_none(&self, db: &'db dyn Db) -> bool {
self.into_nominal_instance()
self.as_nominal_instance()
.is_some_and(|instance| instance.has_known_class(db, KnownClass::NoneType))
}

fn is_bool(&self, db: &'db dyn Db) -> bool {
self.into_nominal_instance()
self.as_nominal_instance()
.is_some_and(|instance| instance.has_known_class(db, KnownClass::Bool))
}

fn is_enum(&self, db: &'db dyn Db) -> bool {
self.into_nominal_instance()
self.as_nominal_instance()
.and_then(|instance| crate::types::enums::enum_metadata(db, instance.class_literal(db)))
.is_some()
}
Expand Down Expand Up @@ -852,7 +852,7 @@ impl<'db> Type<'db> {
}

pub(crate) fn is_notimplemented(&self, db: &'db dyn Db) -> bool {
self.into_nominal_instance()
self.as_nominal_instance()
.is_some_and(|instance| instance.has_known_class(db, KnownClass::NotImplementedType))
}

Expand Down Expand Up @@ -899,7 +899,7 @@ impl<'db> Type<'db> {
) -> Option<Specialization<'db>> {
let class_type = match self {
Type::NominalInstance(instance) => instance,
Type::TypeAlias(alias) => alias.value_type(db).into_nominal_instance()?,
Type::TypeAlias(alias) => alias.value_type(db).as_nominal_instance()?,
_ => return None,
}
.class(db);
Expand Down Expand Up @@ -939,7 +939,7 @@ impl<'db> Type<'db> {
/// I.e., for the type `tuple[int, str]`, this will return the tuple spec `[int, str]`.
/// For a subclass of `tuple[int, str]`, it will return the same tuple spec.
fn tuple_instance_spec(&self, db: &'db dyn Db) -> Option<Cow<'db, TupleSpec<'db>>> {
self.into_nominal_instance()
self.as_nominal_instance()
.and_then(|instance| instance.tuple_spec(db))
}

Expand All @@ -954,7 +954,7 @@ impl<'db> Type<'db> {
/// I.e., for the type `tuple[int, str]`, this will return the tuple spec `[int, str]`.
/// But for a subclass of `tuple[int, str]`, it will return `None`.
fn exact_tuple_instance_spec(&self, db: &'db dyn Db) -> Option<Cow<'db, TupleSpec<'db>>> {
self.into_nominal_instance()
self.as_nominal_instance()
.and_then(|instance| instance.own_tuple_spec(db))
}

Expand Down Expand Up @@ -1044,7 +1044,7 @@ impl<'db> Type<'db> {
}

#[track_caller]
pub(crate) fn expect_class_literal(self) -> ClassLiteral<'db> {
pub(crate) const fn expect_class_literal(self) -> ClassLiteral<'db> {
self.as_class_literal()
.expect("Expected a Type::ClassLiteral variant")
}
Expand All @@ -1058,7 +1058,7 @@ impl<'db> Type<'db> {
matches!(self, Type::ClassLiteral(..))
}

pub(crate) fn as_enum_literal(self) -> Option<EnumLiteralType<'db>> {
pub(crate) const fn as_enum_literal(self) -> Option<EnumLiteralType<'db>> {
match self {
Type::EnumLiteral(enum_literal) => Some(enum_literal),
_ => None,
Expand All @@ -1067,7 +1067,7 @@ impl<'db> Type<'db> {

#[cfg(test)]
#[track_caller]
pub(crate) fn expect_enum_literal(self) -> EnumLiteralType<'db> {
pub(crate) const fn expect_enum_literal(self) -> EnumLiteralType<'db> {
self.as_enum_literal()
.expect("Expected a Type::EnumLiteral variant")
}
Expand All @@ -1076,7 +1076,7 @@ impl<'db> Type<'db> {
matches!(self, Type::TypedDict(..))
}

pub(crate) fn as_typed_dict(self) -> Option<TypedDictType<'db>> {
pub(crate) const fn as_typed_dict(self) -> Option<TypedDictType<'db>> {
match self {
Type::TypedDict(typed_dict) => Some(typed_dict),
_ => None,
Expand Down Expand Up @@ -1126,7 +1126,7 @@ impl<'db> Type<'db> {

#[cfg(test)]
#[track_caller]
pub(crate) fn expect_union(self) -> UnionType<'db> {
pub(crate) const fn expect_union(self) -> UnionType<'db> {
self.as_union().expect("Expected a Type::Union variant")
}

Expand Down Expand Up @@ -4332,7 +4332,7 @@ impl<'db> Type<'db> {
// It will need a special handling, so it remember the origin type to properly
// resolve the attribute.
if matches!(
self.into_nominal_instance()
self.as_nominal_instance()
.and_then(|instance| instance.known_class(db)),
Some(KnownClass::ModuleType | KnownClass::GenericAlias)
) {
Expand Down Expand Up @@ -4544,7 +4544,7 @@ impl<'db> Type<'db> {
// if a tuple subclass defines a `__bool__` method with a return type
// that is inconsistent with the tuple's length. Otherwise, the special
// handling for tuples here isn't sound.
if let Some(instance) = self.into_nominal_instance() {
if let Some(instance) = self.as_nominal_instance() {
if let Some(tuple_spec) = instance.tuple_spec(db) {
Ok(tuple_spec.truthiness())
} else if instance.class(db).is_final(db) {
Expand Down
2 changes: 1 addition & 1 deletion crates/ty_python_semantic/src/types/bound_super.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<'db> SuperOwnerKind<'db> {
}
SuperOwnerKind::Instance(instance) => instance
.normalized_impl(db, visitor)
.into_nominal_instance()
.as_nominal_instance()
.map(Self::Instance)
.unwrap_or(Self::Dynamic(DynamicType::Any)),
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ty_python_semantic/src/types/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ impl<'db> InnerIntersectionBuilder<'db> {

_ => {
let known_instance = new_positive
.into_nominal_instance()
.as_nominal_instance()
.and_then(|instance| instance.known_class(db));

if known_instance == Some(KnownClass::Object) {
Expand Down Expand Up @@ -966,7 +966,7 @@ impl<'db> InnerIntersectionBuilder<'db> {
let contains_bool = || {
self.positive
.iter()
.filter_map(|ty| ty.into_nominal_instance())
.filter_map(|ty| ty.as_nominal_instance())
.filter_map(|instance| instance.known_class(db))
.any(KnownClass::is_bool)
};
Expand Down
2 changes: 1 addition & 1 deletion crates/ty_python_semantic/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ impl<'db> Field<'db> {
/// <https://docs.python.org/3/library/dataclasses.html#dataclasses.KW_ONLY>
pub(crate) fn is_kw_only_sentinel(&self, db: &'db dyn Db) -> bool {
self.declared_ty
.into_nominal_instance()
.as_nominal_instance()
.is_some_and(|instance| instance.has_known_class(db, KnownClass::KwOnly))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ty_python_semantic/src/types/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<'db> Type<'db> {
Type::NominalInstance(NominalInstanceType(NominalInstanceInner::ExactTuple(tuple)))
}

pub(crate) const fn into_nominal_instance(self) -> Option<NominalInstanceType<'db>> {
pub(crate) const fn as_nominal_instance(self) -> Option<NominalInstanceType<'db>> {
match self {
Type::NominalInstance(instance_type) => Some(instance_type),
_ => None,
Expand Down
Loading