Skip to content

FIX Metadata Routing tests and record_metadata - #34179

Merged
adrinjalali merged 34 commits into
scikit-learn:mainfrom
StefanieSenger:fix_record_metadata
Jun 10, 2026
Merged

adrinjalali merged 34 commits into
scikit-learn:mainfrom
StefanieSenger:fix_record_metadata

Conversation

@StefanieSenger

@StefanieSenger StefanieSenger commented Jun 2, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

superseding #33285 (see comment#r3333755620)

What does this implement/fix? Explain your changes.

A central facility of the common metadata tests was broken (we had accidentally compared if an empty list is part of an expected list).

This PR fixes

  1. the test by asserting if records is not empty in check_recorded_metadata; since now a bunch of the tests failed this PR further fixes:
  2. record_metadata which had derived the names of the caller and callee functions too statically. It now takes an educated guess and only uses positional function names as a fallback
  3. skips the one-to-one tests for metadata if metadata is modified (if not preserves_metadata: continue)
  4. add _get_callee_from_caller helper function based on router for tests
  5. adds a filter_registry for metaestimators that do not use every fitted ConsumingEstimtator in also predict and score
  6. clear _records of consuming_metric between test runs
  7. other test inconsistencies that became apparent only after the other test fixes

This PR also adds __repr__ for _Registry class for debugging convenience.

AI usage disclosure

I used AI assistance for:

  • Research and understanding

@@ -407,10 +407,9 @@ def test_nested_routing():

pipeline.predict(X, sample_weight=w3)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most probably, the intend was to test innermost_estimator.predict below here. So I fixed this to test that.

@StefanieSenger StefanieSenger added No Changelog Needed Metadata Routing all issues related to metadata routing, slep006, sample props module:test-suite everything related to our tests labels Jun 2, 2026
@StefanieSenger

StefanieSenger commented Jun 3, 2026 •

Copy link
Copy Markdown
Member Author

Comment on lines +1 to +3
- :class:`ensemble.BaggingClassifier` now correctly routes metadata via `predict` and
`predict_proba` dynamically to the corresponding method on the sub-estimator.
By :user:`Stefanie Senger <StefanieSenger>`.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refers to PR #34188, where I had forgotten the changelog.

"preserves_metadata": "subset",
"scorer_name": "scoring",
"scorer_routing_methods": ["fit", "score"],
"scorer_routing_methods": ["fit"],

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TunedThresholdClassifierCV doesn't route via score.

@StefanieSenger
StefanieSenger marked this pull request as ready for review June 7, 2026 11:15
@StefanieSenger

StefanieSenger commented Jun 7, 2026 •

Copy link
Copy Markdown
Member Author

This is now ready for review. :) @adrinjalali, @OmarManzoor, maybe you want to have a look?

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the help of Claude, here are a few notes which I think are pretty valid.

Comment thread sklearn/tests/test_pipeline.py Resolved
Comment on lines +2633 to +2635
# clear module-level consuming_metric._records (ConsumingScorer) each iteration
if hasattr(consuming_metric, "_records"):
consuming_metric._records.clear()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't we get rid of the module level records?

@StefanieSenger StefanieSenger Jun 10, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea we did. I just forgot the remove them. They're gone now.

Comment on lines +941 to +943
# clear module-level consuming_metric._records (ConsumingScorer) each iteration
if hasattr(consuming_metric, "_records"):
consuming_metric._records.clear()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Comment on lines +2738 to +2741
# clear module-level consuming_metric._records (ConsumingScorer) each iteration
if hasattr(consuming_metric, "_records"):
consuming_metric._records.clear()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here?

Comment thread sklearn/tests/test_pipeline.py Resolved

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you @StefanieSenger

@adrinjalali

Copy link
Copy Markdown
Member

Happy to merge, could you just validate the codecov warnings and check if we should explicitly ignore them, or add a test?

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have both, but a lot more of no cover and I find it nicer 😅

@adrinjalali
adrinjalali merged commit 8883674 into scikit-learn:main Jun 10, 2026
33 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs Jun 10, 2026
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Metadata routing Jun 10, 2026
@StefanieSenger
StefanieSenger deleted the fix_record_metadata branch June 10, 2026 14:29
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks done
jeremiedbb pushed a commit that referenced this pull request Sep 10, 2026
Co-authored-by: Adrin Jalali <[email protected]>
Co-authored-by: Omar Salman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Metadata Routing all issues related to metadata routing, slep006, sample props module:test-suite everything related to our tests No Changelog Needed

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants