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

Code before:
    ///
    /// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
    pub(crate) fn try_to_class_literal(self, db: &dyn Db) -> Option<ClassLiteral<'_>> {
        // a cache of the `KnownClass`es that we have already failed to lookup in typeshed
        // (and therefore that we've already logged a warning for)
        static MESSAGES: LazyLock<Mutex<FxHashSet<KnownClass>>> = LazyLock::new(Mutex::default);

        self.try_to_class_literal_without_logging(db)
            .or_else(|lookup_error| {
                if MESSAGES.lock().unwrap().insert(self) {
                    if matches!(
                        lookup_error,
                        KnownClassLookupError::ClassPossiblyUnbound { .. }
                    ) {
                        tracing::info!("{}", lookup_error.display(db, self));
                    } else {
                        tracing::info!(
                            "{}. Falling back to `Unknown` for the symbol instead.",
                            lookup_error.display(db, self)
                        );
                    }
                }

                match lookup_error {
                    KnownClassLookupError::ClassPossiblyUnbound { class_literal, .. } => {
                        Ok(class_literal)
                    }
                    KnownClassLookupError::ClassNotFound { .. }
                    | KnownClassLookupError::SymbolNotAClass { .. } => Err(()),
                }
            })
            .ok()
    }

    /// Lookup a [`KnownClass`] in typeshed and return a [`Type`] representing that class-literal.
    ///
    /// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
    pub(crate) fn to_class_literal(self, db: &dyn Db) -> Type<'_> {
        self.try_to_class_literal(db)
            .map(Type::ClassLiteral)
            .unwrap_or_else(Type::unknown)
    }

    /// Lookup a [`KnownClass`] in typeshed and return a [`Type`]
    /// representing that class and all possible subclasses of the class.
    ///
    /// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
    pub fn to_subclass_of(self, db: &dyn Db) -> Type<'_> {
        self.to_class_literal(db)
            .to_class_type(db)
            .map(|class| SubclassOfType::from(db, class))

Review comment (suggestion):
I don't think this will be a new issue but we might end up seeing this log message when we hit a salsa cycle because the cycle initial function returns `None`. I can't think of an easy way to avoid this AND keeping the log message.

Code after:
    ///
    /// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
    pub(crate) fn try_to_class_literal(self, db: &dyn Db) -> Option<ClassLiteral<'_>> {
        #[salsa::interned(heap_size=ruff_memory_usage::heap_size)]
        struct KnownClassArgument {
            class: KnownClass,
        }

        fn known_class_to_class_literal_initial<'db>(
            _db: &'db dyn Db,
            _id: salsa::Id,
            _class: KnownClassArgument<'db>,
        ) -> Option<ClassLiteral<'db>> {
            None
        }

        #[salsa::tracked(cycle_initial=known_class_to_class_literal_initial, heap_size=ruff_memory_usage::heap_size)]
        fn known_class_to_class_literal<'db>(
            db: &'db dyn Db,
            class: KnownClassArgument<'db>,
        ) -> Option<ClassLiteral<'db>> {
            let class = class.class(db);
            class
                .try_to_class_literal_without_logging(db)
                .or_else(|lookup_error| {
                    if matches!(
                        lookup_error,
                        KnownClassLookupError::ClassPossiblyUnbound { .. }
                    ) {
                        tracing::info!("{}", lookup_error.display(db, class));
                    } else {
                        tracing::info!(
                            "{}. Falling back to `Unknown` for the symbol instead.",
                            lookup_error.display(db, class)
                        );
                    }

                    match lookup_error {
                        KnownClassLookupError::ClassPossiblyUnbound { class_literal, .. } => {
                            Ok(class_literal)
                        }
                        KnownClassLookupError::ClassNotFound { .. }
                        | KnownClassLookupError::SymbolNotAClass { .. } => Err(()),
                    }
                })
                .ok()
        }

        known_class_to_class_literal(db, KnownClassArgument::new(db, self))
    }