def _apply_scorer(estimator, X, y, scorer, sample_weight):
    """Applies the scorer to the estimator, given the data and sample_weight.

    If ``sample_weight`` is None ``sample_weight`` WILL
    NOT be passed to ``scorer``; otherwise, it will be passed.

    In the event that ``sample_weight`` is provided and used but ``scorer``
    doesn't accept a ``sample_weight`` parameter, then a ``TypeError`` should
    likely be raised.

    Parameters
    ----------
    estimator : estimator object implementing 'fit'
        The object that was used to fit the data.

    X : array-like of shape at least 2D
        The data to fit.

    y : array-like
        The target variable to try to predict in the case of
        supervised learning.  (May be None)

    scorer : A single callable.
        Should return a single float.

        The callable object / fn should have signature
        ``scorer(estimator, X, y, sample_weight=None)`` if ``sample_weight``.

    sample_weight : array-like, shape (y)
        sample weights to use during metric calculation.  May be None.

    Returns
    -------
    score : float
        Score returned by ``scorer`` applied to ``X`` and ``y`` given
        ``sample_weight``.
    """
    if sample_weight is None:
        if y is None:
            score = scorer(estimator, X)
        else:
            score = scorer(estimator, X, y)
    else:
        try:
            # Explicitly force the sample_weight parameter so that an error
            # will be raised in the event that the scorer doesn't take a
            # sample_weight argument.  This is preferable to passing it as
            # a keyword args dict in the case that it just ignores parameters
            # that are not accepted by the scorer.
            if y is None:
                score = scorer(estimator, X, sample_weight=sample_weight)
            else:
                score = scorer(estimator, X, y, sample_weight=sample_weight)
        except TypeError as e:
            if "sample_weight" in str(e):
                raise TypeError(
                    (
                        "Attempted to use 'sample_weight' for training "
                        "but supplied a scorer that doesn't accept a "
                        "'sample_weight' parameter."
                    ),
                    e,
                )
            else:
                raise e
    return score