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

Code before:
            file_scope_id,
            index,
        );

        let mut raw_signature = Signature::from_function(
            db,
            pep695_ctx,
            definition,
            function_stmt_node,
            has_implicitly_positional_first_parameter,
        );

        let generic_context = raw_signature.generic_context;
        raw_signature.add_implicit_self_annotation(db, || {
            if self.is_staticmethod(db) || self.is_classmethod(db) {
                return None;
            }

            let method_may_be_generic = generic_context
                .is_some_and(|context| context.variables(db).any(|v| v.typevar(db).is_self(db)));

            let class_scope_id = definition.scope(db);
            let class_scope = index.scope(class_scope_id.file_scope_id(db));
            let class_node = class_scope.node().as_class()?;
            let class_def = index.expect_single_definition(class_node);
            let (class_literal, class_is_generic) = match infer_definition_types(db, class_def)
                .declaration_type(class_def)
                .inner_type()
            {
                Type::ClassLiteral(class_literal) => {
                    (class_literal, class_literal.generic_context(db).is_some())
                }
                Type::GenericAlias(alias) => (alias.origin(db), true),
                _ => return None,
            };

            if method_may_be_generic
                || class_is_generic
                || class_literal
                    .known(db)
                    .is_some_and(KnownClass::is_fallback_class)
            {
                let scope_id = definition.scope(db);
                let typevar_binding_context = Some(definition);
                let index = semantic_index(db, scope_id.file(db));
                let class = nearest_enclosing_class(db, index, scope_id).unwrap();

                Some(
                    typing_self(db, scope_id, typevar_binding_context, class)
                        .map(Type::TypeVar)
                        .expect(

Review comment (suggestion):
This `infer_definition_types` is okay to keep, since it's referring to the containing class definition, not to the function we're in the middle of inferring a signature for

Code after:
            function_stmt_node,
            file_scope_id,
            index,
        );

        let mut raw_signature = Signature::from_function(
            db,
            pep695_ctx,
            definition,
            function_stmt_node,
            has_implicitly_positional_first_parameter,
        );

        let generic_context = raw_signature.generic_context;
        raw_signature.add_implicit_self_annotation(db, || {
            if self.is_staticmethod(db) {
                return None;
            }

            // We have not yet added an implicit annotation to the `self` parameter, so any
            // typevars that currently appear in the method's generic context come from explicit
            // annotations.
            let method_has_explicit_self = generic_context
                .is_some_and(|context| context.variables(db).any(|v| v.typevar(db).is_self(db)));

            let class_scope_id = definition.scope(db);
            let class_scope = index.scope(class_scope_id.file_scope_id(db));
            let class_node = class_scope.node().as_class()?;
            let class_def = index.expect_single_definition(class_node);
            let Type::ClassLiteral(class_literal) = infer_definition_types(db, class_def)
                .declaration_type(class_def)
                .inner_type()
            else {
                return None;
            };
            let class_is_generic = class_literal.generic_context(db).is_some();
            let class_is_fallback = class_literal
                .known(db)
                .is_some_and(KnownClass::is_fallback_class);

            // Normally we implicitly annotate `self` or `cls` with `Self` or `type[Self]`, and
            // create a `Self` typevar that we then have to solve for whenever this method is
            // called. As an optimization, we can skip creating that typevar in certain situations:
            //
            //   - The method cannot use explicit `Self` in any other parameter annotations,
            //     or in its return type. If it does, then we really do need specialization
            //     inference at each call site to see which specific instance type should be
            //     used in those other parameters / return type.
            //
            //   - The class cannot be generic. If it is, then we might need an actual `Self`
            //     typevar to help carry through constraints that relate the instance type to