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

Code before:
            .fields(db, spec, policy)
            .iter()
            .filter(|(_, field)| {
                matches!(
                    field.kind,
                    FieldKind::Dataclass {
                        init_only: true,
                        ..
                    }
                )
            });

        let first_parameter = Parameter::positional_only(Some(Name::new_static("self")))
            .with_annotated_type(Type::instance(db, class));

        let following_parameters = init_var_fields.map(|(name, field)| {
            Parameter::positional_only(Some(name.clone())).with_annotated_type(field.declared_ty)
        });

        let parameters = Parameters::new(
            db,
            std::iter::once(first_parameter).chain(following_parameters),
        );

        let expected_signature =
            CallableType::single(db, Signature::new(parameters, Type::object()));

        if member
            .ty
            .is_assignable_to(db, Type::Callable(expected_signature))
        {
            return;
        }

        let Some(builder) = context.report_lint(
            &INVALID_DATACLASS,
            definition.focus_range(db, context.module()),
        ) else {
            return;
        };

        let mut diagnostic = builder.into_diagnostic(format_args!(
            "Invalid `__post_init__` signature for dataclass `{}`",
            class.name(db)
        ));
        diagnostic.info(
            "`__post_init__` methods must accept all `InitVar` fields \
            as positional-only parameters",
        );
    }
}

Review comment (suggestion):
`Type::object` here is to support the "any return type is allowed" part of your test suite, right? If so, I think `Type::any()` better models that.

Code after:
    }
    let Some((static_class, spec)) = class.static_class_literal(db) else {
        return;
    };

    let init_var_fields = static_class
        .fields(db, spec, policy)
        .iter()
        .filter(|(_, field)| {
            matches!(
                field.kind,
                FieldKind::Dataclass {
                    init_only: true,
                    ..
                }
            )
        });

    let first_parameter = Parameter::positional_only(Some(Name::new_static("self")))
        .with_annotated_type(Type::instance(db, class));

    let following_parameters = init_var_fields.map(|(name, field)| {
        Parameter::positional_only(Some(name.clone())).with_annotated_type(field.declared_ty)
    });

    let parameters = Parameters::new(
        db,
        std::iter::chain([first_parameter], following_parameters),
    );

    let expected_signature = CallableType::single(db, Signature::new(parameters, Type::object()));

    if member
        .ty
        .is_assignable_to(db, Type::Callable(expected_signature))
    {
        return;
    }

    let Some(builder) = context.report_lint(
        &INVALID_DATACLASS,
        definition.focus_range(db, context.module()),
    ) else {
        return;
    };

    let mut diagnostic = builder.into_diagnostic(format_args!(
        "Invalid `__post_init__` signature for dataclass `{}`",
        class.name(db)
    ));
    diagnostic.info(