Repository: scikit-learn/scikit-learn
File: sklearn/metrics/_classification.py (Python)

Code before:
    check_consistent_length(y_prob, y_true, sample_weight)
    if sample_weight is not None:
        _check_sample_weight(sample_weight, y_prob, force_float_dtype=False)

    y_type = type_of_target(y_true, input_name="y_true")
    if y_type != "binary":
        raise ValueError(
            f"The type of the target inferred from y_true is {y_type} but should be "
            "binary according to the shape of y_prob."
        )

    xp, _, device_ = get_namespace_and_device(y_prob)
    if xp.max(y_prob) > 1:
        raise ValueError(f"y_prob contains values greater than 1: {xp.max(y_prob)}")
    if xp.min(y_prob) < 0:
        raise ValueError(f"y_prob contains values less than 0: {xp.min(y_prob)}")

    # check that pos_label is consistent with y_true
    try:
        pos_label = _check_pos_label_consistency(pos_label, y_true)
    except ValueError:
        xp_y_true, _ = get_namespace(y_true)
        classes = xp_y_true.unique_values(y_true)
        if not (_is_numpy_namespace(xp_y_true) and classes.dtype.kind in "OUS"):
            # For backward compatibility, if classes are not string then
            # `pos_label` will correspond to the greater label.
            # Sort first, as array API spec does not specify order or unique values
            classes = xp_y_true.sort(classes)
            pos_label = classes[-1]
        else:
            raise

    # convert (n_samples,) to (n_samples, 2) shape
    transformed_labels = _one_hot_encoding_binary_target(
        y_true=y_true, pos_label=pos_label, target_xp=xp, target_device=device_
    )
    y_prob = xp.stack((1 - y_prob, y_prob), axis=1)

    return transformed_labels, y_prob


@validate_params(
    {
        "y_true": ["array-like"],
        "y_proba": ["array-like"],
        "sample_weight": ["array-like", None],
        "pos_label": [Real, str, "boolean", None],
        "labels": ["array-like", None],
        "scale_by_half": ["boolean", StrOptions({"auto"})],
    },
    prefer_skip_nested_validation=True,

Review comment (suggestion):
Note this relies on lazy evaluation - if `_is_numpy_namespace(xp_y_true)` is False, the second condition is not evaluated.

Also note when namespace is not numpy, this evaluates to True.

Code after:
    check_consistent_length(y_prob, y_true, sample_weight)
    if sample_weight is not None:
        _check_sample_weight(sample_weight, y_prob, force_float_dtype=False)

    y_type = type_of_target(y_true, input_name="y_true")
    if y_type != "binary":
        raise ValueError(
            f"The type of the target inferred from y_true is {y_type} but should be "
            "binary according to the shape of y_prob."
        )

    xp, _, device_ = get_namespace_and_device(y_prob)
    if xp.max(y_prob) > 1:
        raise ValueError(f"y_prob contains values greater than 1: {xp.max(y_prob)}")
    if xp.min(y_prob) < 0:
        raise ValueError(f"y_prob contains values less than 0: {xp.min(y_prob)}")

    # check that pos_label is consistent with y_true
    try:
        pos_label = _check_pos_label_consistency(pos_label, y_true)
    except ValueError:
        xp_y_true, _ = get_namespace(y_true)
        classes = xp_y_true.unique_values(y_true)
        # For backward compatibility, if classes are not string then
        # `pos_label` will correspond to the greater label.
        if not (_is_numpy_namespace(xp_y_true) and classes.dtype.kind in "OUS"):
            pos_label = classes[-1]
        else:
            raise

    # convert (n_samples,) to (n_samples, 2) shape
    transformed_labels = _one_hot_encoding_binary_target(
        y_true=y_true, pos_label=pos_label, target_xp=xp, target_device=device_
    )
    y_prob = xp.stack((1 - y_prob, y_prob), axis=1)

    return transformed_labels, y_prob


@validate_params(
    {
        "y_true": ["array-like"],
        "y_proba": ["array-like"],
        "sample_weight": ["array-like", None],
        "pos_label": [Real, str, "boolean", None],
        "labels": ["array-like", None],
        "scale_by_half": ["boolean", StrOptions({"auto"})],
    },
    prefer_skip_nested_validation=True,
)
def brier_score_loss(