Skip to content

check_search_not_fitted_error

yohou.testing.check_search_not_fitted_error(search_cv, y, X_actual=None)

Check fitted attributes before fit() raise NotFittedError.

Accessing fitted attributes before fit() must raise NotFittedError. Calling predict() before fit() must raise NotFittedError or AttributeError (AttributeError when refit=False is also accepted).

Parameters

Name Type Description Default
search_cv BaseSearchCV

Unfitted search CV instance

required
y DataFrame

Test target data

required
X_actual DataFrame

Test features

None

Raises

Type Description
AssertionError

If neither NotFittedError nor (for predict) AttributeError is raised

Source Code

Source code in src/yohou/testing/search.py
def check_search_not_fitted_error(search_cv, y: pl.DataFrame, X_actual: pl.DataFrame | None = None) -> None:
    """Check fitted attributes before fit() raise NotFittedError.

    Accessing fitted attributes before fit() must raise ``NotFittedError``.
    Calling ``predict()`` before fit() must raise ``NotFittedError`` or
    ``AttributeError`` (``AttributeError`` when ``refit=False`` is also
    accepted).

    Parameters
    ----------
    search_cv : BaseSearchCV
        Unfitted search CV instance
    y : pl.DataFrame
        Test target data
    X_actual : pl.DataFrame, optional
        Test features

    Raises
    ------
    AssertionError
        If neither NotFittedError nor (for predict) AttributeError is raised

    """
    search_cv_clone = clone(search_cv)

    # Should raise NotFittedError when checking if fitted
    try:
        check_is_fitted(search_cv_clone, "best_forecaster_")
        raise AssertionError(
            f"{search_cv_clone.__class__.__name__} should raise NotFittedError "
            f"when accessing best_forecaster_ before fit()"
        )
    except NotFittedError:
        # Expected behavior
        pass

    # Should raise NotFittedError when calling predict before fit
    try:
        search_cv_clone.predict(forecasting_horizon=1)
        raise AssertionError(
            f"{search_cv_clone.__class__.__name__} should raise NotFittedError when calling predict() before fit()"
        )
    except (NotFittedError, AttributeError):
        # Expected behavior (AttributeError if refit=False)
        pass