Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/types/bound_super.rs (Rust)

Code before:
                }
            }
        }
    }

    fn into_class(self, db: &'db dyn Db) -> Option<ClassType<'db>> {
        match self {
            SuperOwnerKind::Dynamic(_) => None,
            SuperOwnerKind::Class(class) => Some(class),
            SuperOwnerKind::Instance(instance) => Some(instance.class(db)),
            SuperOwnerKind::InstanceTypeVar(bound_typevar)
            | SuperOwnerKind::ClassTypeVar(bound_typevar) => {
                // Return the upper bound's class for subclass checking.
                match bound_typevar.typevar(db).bound_or_constraints(db) {
                    Some(TypeVarBoundOrConstraints::UpperBound(bound)) => match bound {
                        Type::NominalInstance(instance) => Some(instance.class(db)),
                        _ => bound.to_class_type(db),
                    },
                    _ => KnownClass::Object.to_class_literal(db).to_class_type(db),
                }
            }
        }
    }

    /// Returns the type to use when binding methods via the descriptor protocol.
    pub(super) fn binding_type(self, db: &'db dyn Db) -> Type<'db> {
        match self {
            SuperOwnerKind::Dynamic(dynamic) => Type::Dynamic(dynamic),
            SuperOwnerKind::Class(class) => class.into(),
            SuperOwnerKind::Instance(instance) => instance.into(),
            SuperOwnerKind::InstanceTypeVar(bound_typevar) => Type::TypeVar(bound_typevar),
            SuperOwnerKind::ClassTypeVar(bound_typevar) => {
                SubclassOfType::from(db, SubclassOfInner::TypeVar(bound_typevar))
            }
        }
    }
}

/// Represent a bound super object like `super(PivotClass, owner)`
#[salsa::interned(debug, heap_size=ruff_memory_usage::heap_size)]
pub struct BoundSuperType<'db> {
    pub pivot_class: ClassBase<'db>,
    pub owner: SuperOwnerKind<'db>,
}

// The Salsa heap is tracked separately.
impl get_size2::GetSize for BoundSuperType<'_> {}

pub(super) fn walk_bound_super_type<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
    db: &'db dyn Db,
    bound_super: BoundSuperType<'db>,

Review comment (suggestion):
The name and docstring here seem misleading, given that I see this method used more generally (e.g. in `walk_bound_super_type` below)

Code after:
    fn into_class(self, db: &'db dyn Db) -> Option<ClassType<'db>> {
        match self {
            SuperOwnerKind::Dynamic(_) => None,
            SuperOwnerKind::Class(class) => Some(class),
            SuperOwnerKind::Instance(instance) => Some(instance.class(db)),
            SuperOwnerKind::InstanceTypeVar(_, class) | SuperOwnerKind::ClassTypeVar(_, class) => {
                Some(class)
            }
        }
    }

    /// Returns the `TypeVar` instance if this owner is a `TypeVar` variant.
    fn typevar(self, db: &'db dyn Db) -> Option<TypeVarInstance<'db>> {
        match self {
            SuperOwnerKind::InstanceTypeVar(bound_typevar, _)
            | SuperOwnerKind::ClassTypeVar(bound_typevar, _) => Some(bound_typevar.typevar(db)),
            _ => None,
        }
    }

    /// Returns the type representation of this owner.
    pub(super) fn owner_type(self, db: &'db dyn Db) -> Type<'db> {
        match self {
            SuperOwnerKind::Dynamic(dynamic) => Type::Dynamic(dynamic),
            SuperOwnerKind::Class(class) => class.into(),
            SuperOwnerKind::Instance(instance) => instance.into(),
            SuperOwnerKind::InstanceTypeVar(bound_typevar, _) => Type::TypeVar(bound_typevar),
            SuperOwnerKind::ClassTypeVar(bound_typevar, _) => {
                SubclassOfType::from(db, SubclassOfInner::TypeVar(bound_typevar))
            }
        }
    }
}

/// Represent a bound super object like `super(PivotClass, owner)`
#[salsa::interned(debug, heap_size=ruff_memory_usage::heap_size)]
pub struct BoundSuperType<'db> {
    pub pivot_class: ClassBase<'db>,
    pub owner: SuperOwnerKind<'db>,
}

// The Salsa heap is tracked separately.
impl get_size2::GetSize for BoundSuperType<'_> {}

pub(super) fn walk_bound_super_type<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
    db: &'db dyn Db,
    bound_super: BoundSuperType<'db>,
    visitor: &V,
) {
    visitor.visit_type(db, Type::from(bound_super.pivot_class(db)));
    visitor.visit_type(db, bound_super.owner(db).owner_type(db));