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

Code before:
        }

        member
    }

    /// Returns the type of a synthesized dataclass member like `__init__` or `__lt__`, or
    /// a synthesized `__new__` method for a `NamedTuple`.
    pub(super) fn own_synthesized_member(
        self,
        db: &'db dyn Db,
        specialization: Option<Specialization<'db>>,
        inherited_generic_context: Option<GenericContext<'db>>,
        name: &str,
    ) -> Option<Type<'db>> {
        let dataclass_params = self.dataclass_params(db);

        // Handle `@functools.total_ordering`: synthesize comparison methods
        // for classes that have `@total_ordering` and define at least one
        // ordering method. The decorator requires at least one of __lt__,
        // __le__, __gt__, or __ge__ to be defined (either in this class or
        // inherited from a superclass, excluding `object`).
        if self.total_ordering(db) && matches!(name, "__lt__" | "__le__" | "__gt__" | "__ge__") {
            if self.has_ordering_method_in_mro(db, specialization) {
                if let Some(root_method_ty) = self.total_ordering_root_method(db, specialization)
                    && let Some(callables) = root_method_ty.try_upcast_to_callable(db)
                {
                    let bool_ty = KnownClass::Bool.to_instance(db);
                    let synthesized_callables = callables.map(|callable| {
                        let signatures = CallableSignature::from_overloads(
                            callable.signatures(db).iter().map(|signature| {
                                Signature::new_generic(
                                    signature.generic_context,
                                    signature.parameters().clone(),
                                    bool_ty,
                                )
                            }),
                        );
                        CallableType::new(db, signatures, CallableTypeKind::FunctionLike)
                    });

                    return Some(synthesized_callables.into_type(db));
                }
            }
        }

        let field_policy = CodeGeneratorKind::from_class(db, self, specialization)?;

        let mut transformer_params =
            if let CodeGeneratorKind::DataclassLike(Some(transformer_params)) = field_policy {
                Some(DataclassParams::from_transformer_params(
                    db,

Review comment (suggestion):
this let-chain is decent but I want moar let chain:

```diff
diff --git a/crates/ty_python_semantic/src/types/class.rs b/crates/ty_python_semantic/src/types/class.rs
index e8b1506477..0eb84fbe97 100644
--- a/crates/ty_python_semantic/src/types/class.rs
+++ b/crates/ty_python_semantic/src/types/class.rs
@@ -2484,28 +2484,27 @@ impl<'db> ClassLiteral<'db> {
         // ordering method. The decorator requires at least one of __lt__,
         // __le__, __gt__, or __ge__ to be defined (either in this class or
         // inherited from a superclass, excluding `object`).
-        if self.total_ordering(db) && matches!(name, "__lt__" | "__le__" | "__gt__" | "__ge__") {
-            if self.has_ordering_method_in_mro(db, specialization) {
-                if let Some(root_method_ty) = self.total_ordering_root_method(db, specialization)
-                    && let Some(callables) = root_method_ty.try_upcast_to_callable(db)
-                {
-                    let bool_ty = KnownClass::Bool.to_instance(db);
-                    let synthesized_callables = callables.map(|callable| {
-                        let signatures = CallableSignature::from_overloads(
-                            callable.signatures(db).iter().map(|signature| {
-                                Signature::new_generic(
-                                    signature.generic_context,
-                                    signature.parameters().clone(),
-                                    bool_ty,
-                                )
-                            }),
-                        );
-                        CallableType::new(db, signatures, CallableTypeKind::FunctionLike)
-                    });
+        if self.total_ordering(db)
+            && matches!(name, "__lt__" | "__le__" | "__gt__" | "__ge__")
+            && self.has_ordering_method_in_mro(db, specialization)
+            && let Some(root_method_ty) = self.total_ordering_root_method(db, specialization)
+            && let Some(callables) = root_method_ty.try_upcast_to_callable(db)
+        {
+            let bool_ty = KnownClass::Bool.to_instance(db);
+            let synthesized_callables = callables.map(|callable| {
+                let signatures = CallableSignature::from_overloads(
+                    callable.signatures(db).iter().map(|signature| {
+                        Signature::new_generic(
+                            signature.generic_context,
+                            signature.parameters().clone(),
+                            bool_ty,
+                        )
+                    }),
+                );
+                CallableType::new(db, signatures, CallableTypeKind::FunctionLike)
+            });
 
-                    return Some(synthesized_callables.into_type(db));
-                }
-            }
+            return Some(synthesized_callables.into_type(db));
         }
```

Code after:
        db: &'db dyn Db,
        specialization: Option<Specialization<'db>>,
        inherited_generic_context: Option<GenericContext<'db>>,
        name: &str,
    ) -> Option<Type<'db>> {
        let dataclass_params = self.dataclass_params(db);

        // Handle `@functools.total_ordering`: synthesize comparison methods
        // for classes that have `@total_ordering` and define at least one
        // ordering method. The decorator requires at least one of __lt__,
        // __le__, __gt__, or __ge__ to be defined (either in this class or
        // inherited from a superclass, excluding `object`).
        //
        // Only synthesize methods that are not already defined in the MRO.
        if self.total_ordering(db)
            && matches!(name, "__lt__" | "__le__" | "__gt__" | "__ge__")
            && !self
                .iter_mro(db, specialization)
                .filter_map(ClassBase::into_class)
                .filter(|class| !class.class_literal(db).0.is_known(db, KnownClass::Object))
                .any(|class| {
                    class_member(db, class.class_literal(db).0.body_scope(db), name)
                        .ignore_possibly_undefined()
                        .is_some()
                })
            && self.has_ordering_method_in_mro(db, specialization)
            && let Some(root_method_ty) = self.total_ordering_root_method(db, specialization)
            && let Some(callables) = root_method_ty.try_upcast_to_callable(db)
        {
            let bool_ty = KnownClass::Bool.to_instance(db);
            let synthesized_callables = callables.map(|callable| {
                let signatures = CallableSignature::from_overloads(
                    callable.signatures(db).iter().map(|signature| {
                        // The generated methods return a union of the root method's return type
                        // and `bool`. This is because `@total_ordering` synthesizes methods like:
                        //     def __gt__(self, other): return not (self == other or self < other)
                        // If `__lt__` returns `int`, then `__gt__` could return `int | bool`.
                        let return_ty =
                            UnionType::from_elements(db, [signature.return_ty, bool_ty]);
                        Signature::new_generic(
                            signature.generic_context,
                            signature.parameters().clone(),
                            return_ty,
                        )
                    }),
                );
                CallableType::new(db, signatures, CallableTypeKind::FunctionLike)
            });

            return Some(synthesized_callables.into_type(db));
        }