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

Code before:
                                    inner: declared.with_qualifiers(qualifiers),
                                }
                            }
                        }
                    }
                }

                PlaceAndQualifiers {
                    place: Place::Undefined,
                    qualifiers: _,
                } => {
                    // The attribute is not *declared* in the class body. It could still be declared/bound
                    // in a method.

                    Self::implicit_attribute(db, body_scope, name, MethodDecorator::None)
                }
            }
        } else {
            // This attribute is neither declared nor bound in the class body.
            // It could still be implicitly defined in a method.

            Self::implicit_attribute(db, body_scope, name, MethodDecorator::None)
        }
    }

    fn dataclass_instance_field(self, db: &'db dyn Db, name: &str) -> Option<Field<'db>> {
        let field_policy = CodeGeneratorKind::from_static_class(db, self, None)?;
        if !matches!(field_policy, CodeGeneratorKind::DataclassLike(_)) {
            return None;
        }

        let fields = self.own_fields(db, None, field_policy);
        let field = fields.get(&Name::new(name))?;
        matches!(
            field.kind,
            FieldKind::Dataclass {
                init_only: false,
                ..
            }
        )
        .then_some(field.clone())
        .filter(|field| !field.is_kw_only_sentinel(db))
    }

    pub(super) fn to_non_generic_instance(self, db: &'db dyn Db) -> Type<'db> {
        Type::instance(db, ClassType::NonGeneric(self.into()))
    }

    /// Return this class' involvement in an inheritance cycle, if any.
    ///
    /// A class definition like this will fail at runtime,

Review comment (suggestion):
This will return `None` if something is annotated as `ClassVar`, right? I _think_ variables annotated as `ClassVar` are not considered to be dataclass fields

Also, this method will only return `Some()` if `name` is a dataclass field defined on this class _exactly_ (not if it's a field defined on some superclass of this class), so I think a better name for the method would be

```suggestion
    fn own_dataclass_instance_field(self, db: &'db dyn Db, name: &str) -> Option<Field<'db>> {
```

Also also -- it seems like we never look at the returned value, we only check whether it's `Some()` or `None`? So maybe we could just return `bool`, and avoid some unnecessary `.clone()` calls:

```diff
diff --git a/crates/ty_python_semantic/src/types/class.rs b/crates/ty_python_semantic/src/types/class.rs
index dbc32f008a..4949a9db62 100644
--- a/crates/ty_python_semantic/src/types/class.rs
+++ b/crates/ty_python_semantic/src/types/class.rs
@@ -4623,7 +4623,7 @@ impl<'db> StaticClassLiteral<'db> {
                                     .with_qualifiers(qualifiers),
                                 }
                             }
-                        } else if self.dataclass_instance_field(db, name).is_some()
+                        } else if self.is_own_dataclass_instance_field(db, name)
                             && declared_ty
                                 .class_member(db, "__get__".into())
                                 .place
@@ -4711,23 +4711,25 @@ impl<'db> StaticClassLiteral<'db> {
         }
     }
 
-    fn dataclass_instance_field(self, db: &'db dyn Db, name: &str) -> Option<Field<'db>> {
-        let field_policy = CodeGeneratorKind::from_static_class(db, self, None)?;
+    fn is_own_dataclass_instance_field(self, db: &'db dyn Db, name: &str) -> bool {
+        let Some(field_policy) = CodeGeneratorKind::from_static_class(db, self, None) else {
+            return false;
+        };
         if !matches!(field_policy, CodeGeneratorKind::DataclassLike(_)) {
-            return None;
+            return false;
         }
 
         let fields = self.own_fields(db, None, field_policy);
-        let field = fields.get(&Name::new(name))?;
+        let Some(field) = fields.get(name) else {
+            return false;
+        };
         matches!(
             field.kind,
             FieldKind::Dataclass {
                 init_only: false,
                 ..
             }
-        )
-        .then_some(field.clone())
-        .filter(|field| !field.is_kw_only_sentinel(db))
+        ) && !field.is_kw_only_sentinel(db)
     }
 
```

Code after:
                                inner: declared.with_qualifiers(qualifiers),
                            }
                        } else {
                            if let Some(implicit_ty) = Self::implicit_attribute(
                                db,
                                body_scope,
                                name,
                                MethodDecorator::None,
                            )
                            .inner
                            .place
                            .ignore_possibly_undefined()
                            {
                                Member {
                                    inner: Place::Defined(DefinedPlace {
                                        ty: UnionType::from_elements(
                                            db,
                                            [declared_ty, implicit_ty],
                                        ),
                                        origin: TypeOrigin::Declared,
                                        definedness: declaredness,
                                        widening: Widening::None,
                                    })
                                    .with_qualifiers(qualifiers),
                                }
                            } else {
                                Member {
                                    inner: declared.with_qualifiers(qualifiers),
                                }
                            }
                        }
                    }
                }

                PlaceAndQualifiers {
                    place: Place::Undefined,
                    qualifiers: _,
                } => {
                    // The attribute is not *declared* in the class body. It could still be declared/bound
                    // in a method.

                    Self::implicit_attribute(db, body_scope, name, MethodDecorator::None)
                }
            }
        } else {
            // This attribute is neither declared nor bound in the class body.
            // It could still be implicitly defined in a method.

            Self::implicit_attribute(db, body_scope, name, MethodDecorator::None)
        }
    }