Skip to content

check_search_not_fitted_error

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

Check accessing fitted attributes before fit() raises NotFittedError.

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 NotFittedError is not raised when accessing fitted attributes

Source Code

Show/Hide source
def check_search_not_fitted_error(search_cv, y: pl.DataFrame, X_actual: pl.DataFrame | None = None) -> None:
    """Check accessing fitted attributes before fit() raises NotFittedError.

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

    Raises
    ------
    AssertionError
        If NotFittedError is not raised when accessing fitted attributes

    """
    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