8000 ENH Improved error messages for `UnsetMetadataPassedError` by StefanieSenger · Pull Request #28056 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

ENH Improved error messages for UnsetMetadataPassedError #28056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sklearn/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class UnsetMetadataPassedError(ValueError):
"""Exception class to raise if a metadata is passed which is not explicitly \
requested.
requested (metadata=True) or not requested (metadata=False).

.. versionadded:: 1.3

Expand Down
25 changes: 17 additions & 8 deletions sklearn/model_selection/_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,16 @@ def cross_validate(
# `process_routing` code, we pass `fit` as the caller. However,
# the user is not calling `fit` directly, so we change the message
# to make it more suitable for this case.
unrequested_params = sorted(e.unrequested_params)
raise UnsetMetadataPassedError(
message=(
f"{sorted(e.unrequested_params.keys())} are passed to cross"
" validation but are not explicitly requested or unrequested. See"
" the Metadata Routing User guide"
f"{unrequested_params} are passed to cross validation but are not"
" explicitly set as requested or not requested for cross_validate's"
f" estimator: {estimator.__class__.__name__}. Call"
" `.set_fit_request({{metadata}}=True)` on the estimator for"
f" each metadata in {unrequested_params} that you"
" want to use and `metadata=False` for not using it. See the"
" Metadata Routing User guide"
" <https://scikit-learn.org/stable/metadata_routing.html> for more"
" information."
),
Expand Down Expand Up @@ -1238,13 +1243,17 @@ def cross_val_predict(
# `process_routing` code, we pass `fit` as the caller. However,
# the user is not calling `fit` directly, so we change the message
# to make it more suitable for this case.
unrequested_params = sorted(e.unrequested_params)
raise UnsetMetadataPassedError(
message=(
f"{sorted(e.unrequested_params.keys())} are passed to cross"
" validation but are not explicitly requested or unrequested. See"
" the Metadata Routing User guide"
" <https://scikit-learn.org/stable/metadata_routing.html> for more"
" information."
f"{unrequested_params} are passed to `cross_val_predict` but are"
" not explicitly set as requested or not requested for"
f" cross_validate's estimator: {est AF3A imator.__class__.__name__} Call"
" `.set_fit_request({{metadata}}=True)` on the estimator for"
f" each metadata in {unrequested_params} that you want to use and"
" `metadata=False` for not using it. See the Metadata Routing User"
" guide <https://scikit-learn.org/stable/metadata_routing.html>"
" for more information."
),
unrequested_params=e.unrequested_params,
routed_params=e.routed_params,
Expand Down
2 changes: 1 addition & 1 deletion sklearn/model_selection/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2517,7 +2517,7 @@ def test_groups_with_routing_validation(cv_method):
def test_passed_unrequested_metadata(cv_method):
"""Check that we raise an error when passing metadata that is not
requested."""
err_msg = re.escape("['metadata'] are passed to cross validation")
err_msg = re.escape("but are not explicitly set as requested or not requested")
with pytest.raises(ValueError, match=err_msg):
cv_method(
estimator=ConsumingClassifier(),
Expand Down
0