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

Code before:
    /// The definition where this class is created (if in an assignment context).
    ///
    /// This is `Some` when the `type()` call is part of an assignment,
    /// allowing go-to-definition to navigate to the creation site.
    pub definition: Option<Definition<'db>>,

    /// Dataclass parameters if this class has been wrapped with `@dataclass` decorator
    /// or passed to `dataclass()` as a function.
    pub dataclass_params: Option<DataclassParams<'db>>,
}

impl get_size2::GetSize for DynamicClassLiteral<'_> {}

#[salsa::tracked]
impl<'db> DynamicClassLiteral<'db> {
    /// Returns a [`Span`] with the range of the `type()` call expression.
    ///
    /// See [`Self::header_range`] for more details.
    pub(super) fn header_span(self, db: &'db dyn Db) -> Span {
        Span::from(self.file(db)).with_range(self.header_range(db))
    }

    /// Returns the range of the `type()` call expression that created this class.
    pub(super) fn header_range(self, db: &'db dyn Db) -> TextRange {
        let module = parsed_module(db, self.file(db)).load(db);
        self._node_ref(db).node(&module).range()
    }

    /// Get the metaclass of this dynamic class.
    ///
    /// Derives the metaclass from base classes: finds the most derived metaclass
    /// that is a subclass of all other base metaclasses.
    ///
    /// See <https://docs.python.org/3/reference/datamodel.html#determining-the-appropriate-metaclass>
    pub(crate) fn metaclass(self, db: &'db dyn Db) -> Type<'db> {
        self.try_metaclass(db)
            .unwrap_or_else(|_| SubclassOfType::subclass_of_unknown())
    }

    /// Try to get the metaclass of this dynamic class.
    ///
    /// Returns `Err(DynamicMetaclassConflict)` if there's a metaclass conflict
    /// (i.e., two base classes have metaclasses that are not in a subclass relationship).
    ///
    /// See <https://docs.python.org/3/reference/datamodel.html#determining-the-appropriate-metaclass>
    pub(crate) fn try_metaclass(
        self,
        db: &'db dyn Db,
    ) -> Result<Type<'db>, DynamicMetaclassConflict<'db>> {
        let bases = self.bases(db);


Review comment (suggestion):
Can you add a full snapshot diagnostic test showing how the header range gets highlighted? Because I think we should only highlight the `name` part rather than the entire `CallExression` (which can be very long) 

Code after:
    /// The name of the class (from the first argument to `type()`).
    #[returns(ref)]
    pub name: Name,

    /// The base classes (from the second argument to `type()`).
    #[returns(deref)]
    pub bases: Box<[ClassBase<'db>]>,

    /// The anchor for this dynamic class, providing stable identity.
    ///
    /// - `Definition`: The `type()` call is assigned to a variable. The definition
    ///   uniquely identifies this class and can be used to find the `type()` call.
    /// - `ScopeOffset`: The `type()` call is "dangling" (not assigned). The offset
    ///   is relative to the enclosing scope's anchor node index.
    pub anchor: DynamicClassAnchor<'db>,

    /// The class members from the namespace dict (third argument to `type()`).
    /// Each entry is a (name, type) pair extracted from the dict literal.
    #[returns(deref)]
    pub members: Box<[(Name, Type<'db>)]>,

    /// Whether the namespace dict (third argument) is dynamic (not a literal dict,
    /// or contains non-string-literal keys). When true, attribute lookups on this
    /// class and its instances return `Unknown` instead of failing.
    pub has_dynamic_namespace: bool,

    /// Dataclass parameters if this class has been wrapped with `@dataclass` decorator
    /// or passed to `dataclass()` as a function.
    pub dataclass_params: Option<DataclassParams<'db>>,
}

/// Anchor for identifying a dynamic class literal.
///
/// This enum provides stable identity for `DynamicClassLiteral`:
/// - For assigned calls, the `Definition` uniquely identifies the class.
/// - For dangling calls, a relative offset provides stable identity.
#[derive(
    Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, salsa::Update, get_size2::GetSize,
)]
pub enum DynamicClassAnchor<'db> {
    /// The `type()` call is assigned to a variable.
    ///
    /// The `Definition` uniquely identifies this class. The `type()` call expression
    /// is the `value` of the assignment, so we can get its range from the definition.
    Definition(Definition<'db>),

    /// The `type()` call is "dangling" (not assigned to a variable).
    ///
    /// The offset is relative to the enclosing scope's anchor node index.
    /// For module scope, this is equivalent to an absolute index (anchor is 0).
    ScopeOffset { scope: ScopeId<'db>, offset: u32 },