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

Code before:
                            .when_assignable_to(self.db, bound, self.inferable)
                            .is_always_satisfied(self.db)
                        {
                            return Err(SpecializationError::MismatchedBound {
                                bound_typevar,
                                argument: ty,
                            });
                        }
                        self.add_type_mapping(bound_typevar, ty, polarity, f);
                    }
                    Some(TypeVarBoundOrConstraints::Constraints(constraints)) => {
                        // Prefer an exact match first.
                        for constraint in constraints.elements(self.db) {
                            if ty == *constraint {
                                self.add_type_mapping(bound_typevar, ty, polarity, f);
                                return Ok(());
                            }
                        }

                        // If `ty` is itself a constrained TypeVar, check whether each
                        // of its constraints satisfies at least one constraint of the
                        // formal TypeVar. This handles the case where two TypeVars with
                        // identical (or compatible) constraint sets are used across
                        // function boundaries.
                        if let Type::TypeVar(actual_typevar) = ty {
                            if let Some(actual_constraints) =
                                actual_typevar.typevar(self.db).constraints(self.db)
                            {
                                let all_satisfied =
                                    actual_constraints.iter().all(|actual_constraint| {
                                        constraints.elements(self.db).iter().any(
                                            |formal_constraint| {
                                                actual_constraint
                                                    .is_assignable_to(self.db, *formal_constraint)
                                            },
                                        )
                                    });
                                if all_satisfied {
                                    self.add_type_mapping(bound_typevar, ty, polarity, f);
                                    return Ok(());
                                }
                            }
                        }

                        for constraint in constraints.elements(self.db) {
                            let is_satisfied = if polarity.is_contravariant() {
                                constraint
                                    .when_assignable_to(self.db, ty, self.inferable)
                                    .is_always_satisfied(self.db)
                            } else {
                                ty.when_assignable_to(self.db, *constraint, self.inferable)

Review comment (suggestion):
Nit: you could reduce indentation by using a `let` chain 

Code after:
                            .when_assignable_to(self.db, bound, self.inferable)
                            .is_always_satisfied(self.db)
                        {
                            return Err(SpecializationError::MismatchedBound {
                                bound_typevar,
                                argument: ty,
                            });
                        }
                        self.add_type_mapping(bound_typevar, ty, polarity, f);
                    }
                    Some(TypeVarBoundOrConstraints::Constraints(constraints)) => {
                        // Prefer an exact match first.
                        for constraint in constraints.elements(self.db) {
                            if ty == *constraint {
                                self.add_type_mapping(bound_typevar, ty, polarity, f);
                                return Ok(());
                            }
                        }

                        // If `ty` is itself a constrained TypeVar, check whether each
                        // of its constraints is equivalent to at least one constraint of
                        // the formal TypeVar. This handles the case where two TypeVars
                        // with identical constraint sets are used across function
                        // boundaries.
                        //
                        // We require equivalence rather than assignability to maintain
                        // soundness: constrained TypeVars allow narrowing via
                        // `isinstance` checks inside the function body, so a constraint
                        // that is a strict subtype (e.g. `bool` vs `int`) would allow
                        // the callee to return a widened type that violates the caller's
                        // constraint.
                        if let Type::TypeVar(actual_typevar) = ty
                            && let Some(actual_constraints) =
                                actual_typevar.typevar(self.db).constraints(self.db)
                        {
                            let all_satisfied =
                                actual_constraints.iter().all(|actual_constraint| {
                                    constraints
                                        .elements(self.db)
                                        .iter()
                                        .any(|formal_constraint| {
                                            actual_constraint
                                                .is_equivalent_to(self.db, *formal_constraint)
                                        })
                                });
                            if all_satisfied {
                                self.add_type_mapping(bound_typevar, ty, polarity, f);
                                return Ok(());
                            }
                        }