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

Code before:
                        }
                    }
                    Some(TypeVarBoundOrConstraints::Constraints(constraints)) => {
                        return build_constrained_union(
                            constraints,
                            bound_typevar,
                            typevar,
                            SuperOwnerKind::InstanceTypeVar,
                        );
                    }
                    None => {
                        // No bound means the implicit upper bound is `object`.
                        let class = ClassType::object(db);
                        SuperOwnerKind::InstanceTypeVar(bound_typevar, class)
                    }
                }
            }
            Type::BooleanLiteral(_) | Type::TypeIs(_) | Type::TypeGuard(_) => {
                return delegate_to(KnownClass::Bool.to_instance(db));
            }
            Type::IntLiteral(_) => return delegate_to(KnownClass::Int.to_instance(db)),
            Type::StringLiteral(_) | Type::LiteralString => {
                return delegate_to(KnownClass::Str.to_instance(db));
            }
            Type::BytesLiteral(_) => {
                return delegate_to(KnownClass::Bytes.to_instance(db));
            }
            Type::SpecialForm(special_form) => {
                return delegate_to(special_form.instance_fallback(db));
            }
            Type::KnownInstance(instance) => {
                return delegate_to(instance.instance_fallback(db));
            }
            Type::FunctionLiteral(_) | Type::DataclassDecorator(_) => {
                return delegate_to(KnownClass::FunctionType.to_instance(db));
            }
            Type::WrapperDescriptor(_) => {
                return delegate_to(KnownClass::WrapperDescriptorType.to_instance(db));
            }
            Type::KnownBoundMethod(method) => {
                return delegate_to(method.class().to_instance(db));
            }
            Type::BoundMethod(_) => return delegate_to(KnownClass::MethodType.to_instance(db)),
            Type::ModuleLiteral(_) => {
                return delegate_to(KnownClass::ModuleType.to_instance(db));
            }
            Type::GenericAlias(_) => return delegate_to(KnownClass::GenericAlias.to_instance(db)),
            Type::PropertyInstance(_) => return delegate_to(KnownClass::Property.to_instance(db)),
            Type::EnumLiteral(enum_literal_type) => {
                return delegate_to(enum_literal_type.enum_class_instance(db));
            }

Review comment (refactor):
Here again, I don't think this match arm is right. It's at least not needed to pass any current test. I think if we did support a class-literal type (or subclass-of type) as upper bound here, the correct thing to do might be to create a `ClassTypeVar` then, not an `InstanceTypeVar`? But I think we can leave consideration of that case to a separate PR, if it ever comes up in practice.

```suggestion
                        if let Type::NominalInstance(instance) = bound {
                            SuperOwnerKind::InstanceTypeVar(bound_typevar, instance.class(db))
                        } else {
                            return Err(BoundSuperError::AbstractOwnerType {
                                owner_type,
                                pivot_class: pivot_class_type,
                                typevar_context: Some(typevar),
                            });
                        }

```

Code after:
                for negative in intersection.negative(db) {
                    if let Ok(good_element) = delegate_to(*negative) {
                        builder = builder.add_negative(good_element);
                    }
                }
                return Ok(builder.build());
            }
            Type::TypeAlias(alias) => {
                return delegate_to(alias.value_type(db));
            }
            Type::TypeVar(bound_typevar) => {
                let typevar = bound_typevar.typevar(db);
                match typevar.bound_or_constraints(db) {
                    Some(TypeVarBoundOrConstraints::UpperBound(bound)) => {
                        let class = match bound {
                            Type::NominalInstance(instance) => Some(instance.class(db)),
                            Type::ProtocolInstance(protocol) => protocol
                                .to_nominal_instance()
                                .map(|instance| instance.class(db)),
                            _ => None,
                        };
                        if let Some(class) = class {
                            SuperOwnerKind::InstanceTypeVar(bound_typevar, class)
                        } else {
                            return delegate_with_error_mapped(bound, Some(typevar));
                        }
                    }
                    Some(TypeVarBoundOrConstraints::Constraints(constraints)) => {
                        return build_constrained_union(
                            constraints,
                            bound_typevar,
                            typevar,
                            SuperOwnerKind::InstanceTypeVar,
                        );
                    }
                    None => {
                        // No bound means the implicit upper bound is `object`.
                        SuperOwnerKind::InstanceTypeVar(bound_typevar, ClassType::object(db))
                    }
                }
            }
            Type::BooleanLiteral(_) | Type::TypeIs(_) | Type::TypeGuard(_) => {
                return delegate_to(KnownClass::Bool.to_instance(db));
            }
            Type::IntLiteral(_) => return delegate_to(KnownClass::Int.to_instance(db)),
            Type::StringLiteral(_) | Type::LiteralString => {
                return delegate_to(KnownClass::Str.to_instance(db));
            }
            Type::BytesLiteral(_) => {
                return delegate_to(KnownClass::Bytes.to_instance(db));
            }