8000 ENH Improved error messages for `UnsetMetadataPassedError` (#28056) · scikit-learn/scikit-learn@34061da · GitHub
[go: up one dir, main page]

Skip to content

Commit 34061da

Browse files
ENH Improved error messages for UnsetMetadataPassedError (#28056)
1 parent 3c759c5 commit 34061da

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

sklearn/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
class UnsetMetadataPassedError(ValueError):
2121
"""Exception class to raise if a metadata is passed which is not explicitly \
22-
requested.
22+
requested (metadata=True) or not requested (metadata=False).
2323
2424
.. versionadded:: 1.3
2525

sklearn/model_selection/_validation.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,16 @@ def cross_validate(
397397
# `process_routing` code, we pass `fit` as the caller. However,
398398
# the user is not calling `fit` directly, so we change the message
399399
# to make it more suitable for this case.
400+
unrequested_params = sorted(e.unrequested_params)
400401
raise UnsetMetadataPassedError(
401402
message=(
402-
f"{sorted(e.unrequested_params.keys())} are passed to cross"
403-
" validation but are not explicitly requested or unrequested. See"
404-
" the Metadata Routing User guide"
403+
f"{unrequested_params} are passed to cross validation but are not"
404+
" explicitly set as requested or not requested for cross_validate's"
405+
f" estimator: {estimator.__class__.__name__}. Call"
406+
" `.set_fit_request({{metadata}}=True)` on the estimator for"
407+
f" each metadata in {unrequested_params} that you"
408+
" want to use and `metadata=False` for not using it. See the"
409+
" Metadata Routing User guide"
405410
" <https://scikit-learn.org/stable/metadata_routing.html> for more"
406411
" information."
407412
),
@@ -1238,13 +1243,17 @@ def cross_val_predict(
12381243
# `process_routing` code, we pass `fit` as the caller. However,
12391244
# the user is not calling `fit` directly, so we change the message
12401245
# to make it more suitable for this case.
1246+
unrequested_params = sorted(e.unrequested_params)
12411247
raise UnsetMetadataPassedError(
12421248
message=(
1243-
f"{sorted(e.unrequested_params.keys())} are passed to cross"
1244-
" validation but are not explicitly requested or unrequested. See"
1245-
" the Metadata Routing User guide"
1246-
" <https://scikit-learn.org/stable/metadata_routing.html> for more"
1247-
" information."
1249+
f"{unrequested_params} are passed to `cross_val_predict` but are"
1250+
" not explicitly set as requested or not requested for"
1251+
f" cross_validate's estimator: {estimator.__class__.__name__} Call"
1252+
" `.set_fit_request({{metadata}}=True)` on the estimator for"
1253+
f" each metadata in {unrequested_params} that you want to use and"
1254+
" `metadata=False` for not using it. See the Metadata Routing User"
1255+
" guide <https://scikit-learn.org/stable/metadata_routing.html>"
1256+
" for more information."
12481257
),
12491258
unrequested_params=e.unrequested_params,
12501259
routed_params=e.routed_params,

sklearn/model_selection/tests/test_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2517,7 +2517,7 @@ def test_groups_with_routing_validation(cv_method):
25172517
def test_passed_unrequested_metadata(cv_method):
25182518
"""Check that we raise an error when passing metadata that is not
25192519
requested."""
2520-
err_msg = re.escape("['metadata'] are passed to cross validation")
2520+
err_msg = re.escape("but are not explicitly set as requested or not requested")
25212521
with pytest.raises(ValueError, match=err_msg):
25222522
cv_method(
25232523
estimator=ConsumingClassifier(),

0 commit comments

Comments
 (0)
0