Skip to content

MNT Refactor and deprecate get_metadata_routing method in _MetadataRequester - #31695

Open
StefanieSenger wants to merge 27 commits into
scikit-learn:mainfrom
StefanieSenger:refactor_router_requester
Open

StefanieSenger wants to merge 27 commits into
scikit-learn:mainfrom
StefanieSenger:refactor_router_requester

Conversation

@StefanieSenger

@StefanieSenger StefanieSenger commented Jul 3, 2025 •

Copy link
Copy Markdown
Member

What does this implement/fix? Explain your changes.

This PR de-couples the double-handling of MetadataRouter and MetadataRequest objects in metadata routing ( a bit), by not using the get_metadata_routing method in MetadataRequest anymore, but instead to directly use _get_metadata_request.

This change has affect in several parts of our metadata routing implementation and the tests and there simplifies the code and makes it more intuitive to read.

The goal of this PR is to increase maintainability.

While at it, I also:

  • re-named __metadata_request__* class attribute into __metadata_request__{method} and made clear to mention its a class attribute in our internal documentation to make it easier to distinguish it from the _metadata_request instance attribute present in consumers at first sight
  • made some other small improvements for the external or internal documentation in metadata routing

@github-actions

github-actions Bot commented Jul 3, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 6f97885. Link to the linter CI: here

@StefanieSenger StefanieSenger changed the title Re-factor MNT Refactor double-handling of MetadataRouter and MetadataRequest objects Jul 3, 2025
@StefanieSenger StefanieSenger added Metadata Routing all issues related to metadata routing, slep006, sample props No Changelog Needed and removed module:metrics module:model_selection labels Jul 3, 2025
Comment on lines +996 to +1002
with pytest.warns(UserWarning, match="`transform` method which consumes metadata"):
rct = RoutingCustomTransformer(
estimator=CustomTransformer()
.set_fit_request(metadata=True)
.set_transform_request(metadata=True)
)
rct.fit_transform(X=[[1]], metadata="metadata")

@StefanieSenger StefanieSenger Jul 4, 2025 •

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 test was added to make sure the warning is also shown when a transformer is not a pure consumer (but also a router). It refers to the change in sklearn.base.TransformerMixin.fit_transform.

def test_default_requests():
class OddEstimator(BaseEstimator):
def test_class_level_requests():
class StubEstimator(BaseEstimator):

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.

I'm renaming OddEstimator because the old naming implies we are intentionally demonstrating wrong usage, but that's not our purpose here.

@config_context(enable_metadata_routing=True)
def test_default_requests():
class OddEstimator(BaseEstimator):
def test_class_level_requests():

@StefanieSenger StefanieSenger Jul 4, 2025 •

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.

I'm renaming test_default_requests because the term "default" is not referring to default / auto routing here and could be mis-understood. What is meant are the class level requests.

@StefanieSenger

StefanieSenger commented Jul 5, 2025 •

Copy link
Copy Markdown
Member Author

Log of my though process (that hopefully helps me move forwards):

The de-coupeling of _get_metadata_request for consumers and get_metadata_routing that was previously used in both consumers and routers and is now only used in routers is a separate issue than removing the add_self_request in routers by using their _get_metadata_request method, that is still present in them.
I understand I should do it in the same PR nevertheless.

I wonder if we want to fully automate the setting of self requests, or if it should be integrated in MetadataRouter.add(). But if the latter, I wonder if it really makes things simpler, because we would force estimator developers to define a method_mapping in .add, that is not easily available for self requests (or only in a hacky way).
So I tend to fully automate, which means that I'd have to manipulate MetadataRouter.__init__(). I have hardly seen us add any functionality to any init method in scikit-learn, and I think for a good reason (predictability or so?). Still, I will make a suggestion to automate the setting of self requests for router, because otherwise, we force us and developers of custom meta-estimators to pass a complicated method mapping into MetadataRouter.add()...

Since then I need to pass an object into MetadataRouter.__init__(), so I can create a self request, I wonder if I should make the existing owner become an object type, instead of a string, or if I should add a new init param that would be the object. Maybe better to pass a new param, because then the owner string can still get a user-defined name?

But potentially, those could both be merged into the same param, since passing MetadataRouter(owner=self.__class__.__name__, owning_obj=self) seems a bit unnecessary.

@StefanieSenger

StefanieSenger commented Jul 5, 2025 •

Copy link
Copy Markdown
Member Author

I understood that this PR cannot do much about the double-handling of MetadataRouter and MetadataRequest objects (which I had thought it was intending in the beginning), because that depends on several other places and choices taken. But it does part of the job by taking care of certain methods. I will update the PR title to better fit that.

@StefanieSenger StefanieSenger changed the title MNT Refactor double-handling of MetadataRouter and MetadataRequest objects MNT Refactor get_metadata_routing method in MetadataRequest to directly use _get_metadata_request instead Jul 5, 2025
@StefanieSenger

Copy link
Copy Markdown
Member Author

I've experimented with my above idea to add a self-request automatically to routers, but it fails for routers that are pure routers but not also consumers. Given that, I no longer see a benefit in removing or refining the add_self_request method from MetadataRouter. We would still require some input from estimator developers on whether a self request should be added or not.

If that input is done by calling MetadataRouter().add_self_request(self) in get_metadata_routing() or through - in the simplest case, boolean paramater (like add_self_request=True) feels like an implementation detail.

Also, the original change of this PR, the de-tangling of _get_metadata_request and get_metadata_routing methods for consumers doesn't provide a new possibility here, because MetadataRouter().add_self_request had already internally used obj._get_metadata_request().

At this point, I am tempted to think this refactoring is complete, unless there is some specific thing that we want to do with add_self_request?

This PR makes the code more readable, because we can clearly see if we deal with a consumer based on the method called on an object. It also adds some improvements to the internal and the API docs and personally, it has given me more insights and more ideas for refactorings (which I will open separate PRs for).

What is left that I will go through our docs and examples to check whether the right method is used everywhere. I will do that and then un-draft this PR. Do you consider it complete, @adrinjalali?

MetadataRouter
~~~~~~~~~~~~~~

The ``MetadataRouter`` objects are constructed via a `get_metadata_routing` method,

@StefanieSenger StefanieSenger Jul 5, 2025 •

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.

The MetadataRouter objects are constructed via a get_metadata_routing method

@adrinjalali, Do you think it makes sense to rename get_metadata_routing into get_metadata_router?
That would be more precise and we spare people to wonder about whether there is a difference between "metadata router" and "metadata routing". In fact, this method returns a MetadataRouter object.

That would be for a new PR and could be done without deprecation (because we are still in experimental stage), I believe?

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.

I wouldn't mind the rename, but needs to go through a deprecation cycle. Maybe a different PR.

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.

I think the renaming would be very useful for people unfamiliar with metadata routing like me: _get_metadata_request returns a MetadataRequest, get_metadata_router returns a MetadataRouter, all is clear. So +1 for a follow-up PR with the renaming !

@StefanieSenger
StefanieSenger marked this pull request as ready for review July 5, 2025 17:45
Comment on lines -548 to -568
Consumer-only classes such as simple estimators return a serialized
version of this class as the output of `get_metadata_routing()`.

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.

That's not true (anymore).

get_routing_for_object(), _get_metadata_request() and get_metadata_routing() all return unserialised output.


return requests

def get_metadata_routing(self):

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.

A side effect of this change is that this method won't be displayed in the docs for the consumers anymore. I don't think it's too bad, since we want users to use get_routing_for_object anyways.

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.

I think we should also do a single release deprecation cycle for this (shorter than usual).

@StefanieSenger StefanieSenger left a comment

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.

Thanks for reviewing, @antoinebaker! I have modified a bit according to your review. Awaiting the next pass. :)

``_MetadataRequester`` class, and ``BaseEstimator`` inherits from this mixin.
This mixin also implements the ``get_metadata_routing``, which meta-estimators
need to override, but it works for simple consumers as is.
This mixin also implements a ``_get_metadata_request`` method.

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.

Yes, I think you are right. However, returning the self requests is not the primary purpose of having _get_metadata_request for pure routers.

I will try to explain to the best of my current understanding:

All estimator objects have their own _get_metadata_request method. I think it is easier to just have the method than to define some distinction between routers and consumers, because many can be both, pure routers are seldom and being a consumer depends on whether a set_{method}_request method is set on them or not.

When set_fit_request is called for instance, _get_metadata_request method is used to check whether they could potentially consume metadata by checking the signatures of their methods and adding class level settings (see _get_class_level_metadata_request_values therefore). And thewhile, the estimator builds a _metadata_request attribute that stores all that information. If I understand correctly, this is identical to the self requests.

When we call _get_metadata_request on a router, it returns that stored object. It might have been updated via the user code in the meantime.

So _get_metadata_request can serve two different purposes, and the first one is needed for any estimator, the second one is actually only interesing for consumers.

I also find that very confusing. I wonder if a solution could be to make the if-else distinction in _get_metadata_request in other places instead and reduce the functionality of _get_metadata_request to either only build the MetadataRequest object or to retrieve it. In that case, we could also rename it. What do you think, @antoinebaker and @adrinjalali? (I am thinking about a future PR of cause.)

Comment thread sklearn/utils/_metadata_requests.py Resolved
Comment thread sklearn/utils/_metadata_requests.py Outdated
# for other reasons.
if hasattr(obj, "get_metadata_routing"):

# TODO(1.9): remove when get_metadata_routing is removed

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.

Yes, I will add that. Thanks!

):
raise AttributeError(
f"The given object ({_obj.__class__.__name__!r}) needs to either"
" implement the routing method `get_metadata_routing` or be a"

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.

Oh my, when I added that condition, it had made sense to me and now I cannot explain it anymore ... 😅

I now think you are right and I have removed the new condition, @antoinebaker.

On the question whether we ever call process_routing on MetadataRouter instances: It seems we do that in some of the tests in the test_metaestimators_metadata_routing.py module. But we shouldn't modify code just to match a test. Instead, I think the tests would need to be different / less abstract.

@StefanieSenger

Copy link
Copy Markdown
Member Author

@antoinebaker:

With this PR and after the deprecation of _MetadataRequester.get_metadata_routing, will we able to tell if an object if a router/consumer by duck typing ?

hasattr(obj, "_get_metadata_request") # consumer
hasattr(obj, "get_metadata_routing") # router

No, unfortunately, we won't be able to do that, since both objects will have the _get_metadata_request method and its used for creating a request object as well as to retrieve it. (See this comment.)

It might be that pure routers (that do not have self requests), don't need it and in this case, they could maybe not inherit from the _MetadataRequest mixin class?

@antoinebaker antoinebaker 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.

Here a second pass with minor comments / suggestions !

Comment on lines 227 to +228
assert not len(get_routing_for_object(NonConsumingClassifier()).fit.requests)
assert_request_is_empty(NonConsumingClassifier().get_metadata_routing())
assert_request_is_empty(NonConsumingClassifier()._get_metadata_request())

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.

Maybe the following is clearer

ncc_request = get_routing_for_object(NonConsumingClassifier())
assert ncc_request.fit.requests == {}
assert_request_is_empty(ncc_request)

Comment on lines 2060 to +2064
@pytest.mark.parametrize("cv", ALL_SPLITTERS, ids=[str(cv) for cv in ALL_SPLITTERS])
def test_splitter_get_metadata_routing(cv):
"""Check get_metadata_routing returns the correct MetadataRouter."""
assert hasattr(cv, "get_metadata_routing")
metadata = cv.get_metadata_routing()
assert hasattr(cv, "_get_metadata_request")
metadata = cv._get_metadata_request()

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.

In line with the other test modifications maybe rename:

  • test_splitter_get_metadata_routing -> test_splitter__get_metadata_request
  • "Check _get_metadata_request returns the correct MetadataRequest."
  • metadata -> cv_request

Comment on lines +30 to +32
if (
hasattr(method_obj, "__call__") or isinstance(method_obj, property)
) and not _is_deprecated(method_obj):

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.

Does this mean that we will no longer test the docstrings of deprecated methods ?

):
ConsumingRegressor().get_metadata_routing()

MetaRegressor(estimator=ConsumingRegressor()).get_metadata_routing()

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.

Shouldn't we test here that it does not raise a FutureWarning. Not sure about the best way to check but maybe something like that:

Suggested change
MetaRegressor(estimator=ConsumingRegressor()).get_metadata_routing()
with warnings.catch_warnings(record=True) as record:
MetaRegressor(estimator=ConsumingRegressor()).get_metadata_routing()
assert len(record) == 0

``_MetadataRequester`` class, and ``BaseEstimator`` inherits from this mixin.
This mixin also implements the ``get_metadata_routing``, which meta-estimators
need to override, but it works for simple consumers as is.
This mixin also implements a ``_get_metadata_request`` method.

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.

Not a pb ! Here is the call stack if I understand correctly 🤷

  1. Estimator instance created. _metadata_request does not exist yet.
  2. User first calls set_{method}_request with some keyword arguments.
  3. Descriptor calls _get_metadata_request().
  4. _metadata_request is missing, calls _get_default_requests().
  5. _get_default_requests() builds and returns a MetadataRequest (the class level "default" requests).
  6. The MetadataRequest is updated for the keyword arguments provided by the user.
  7. The MetadataRequest is stored on the _metadata_request attribute of the estimator.
  8. Future calls use the existing _metadata_request attribute.

``_MetadataRequester`` class, and ``BaseEstimator`` inherits from this mixin.
This mixin also implements the ``get_metadata_routing``, which meta-estimators
need to override, but it works for simple consumers as is.
This mixin also implements a ``_get_metadata_request`` method.

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.

But you've answered my question :)
For a router-consumer object,obj._get_metadata_request() returns the self request. It's indeed used within the MetadataRouter add_self_request.

of the output of that method.
of its :class:`~sklearn.utils.metadata_routing.MetadataRouter`.
- If the object provides a `_get_metadata_request` method, return a copy
of is :class:`~sklearn.utils.metadata_routing.MetadataRequest`.

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.

Suggested change
of is :class:`~sklearn.utils.metadata_routing.MetadataRequest`.
of its :class:`~sklearn.utils.metadata_routing.MetadataRequest`.

@antoinebaker

antoinebaker commented Sep 18, 2025 •

Copy link
Copy Markdown
Member

No, unfortunately, we won't be able to do that, since both objects will have the _get_metadata_request method and its used for creating a request object as well as to retrieve it. (See this comment.)

Ah thanks for the clarification! Maybe a workaround to test if an object is not a consumer is:

assert_request_equal(obj._get_metadata_request(), {})

(but I'm still unsure when an object is considered or not a consumer)

@StefanieSenger StefanieSenger self-assigned this Dec 11, 2025
@StefanieSenger StefanieSenger moved this to Todo in Labs Dec 11, 2025
@StefanieSenger StefanieSenger moved this from Todo to In progress in Labs Dec 11, 2025
@StefanieSenger

Copy link
Copy Markdown
Member Author

Note to myself on what to do next: add developer API for _get_metadata_request similar to __sklearn_clone, __sklearn_tags, and __sklearn_isfitted.

@StefanieSenger

Copy link
Copy Markdown
Member Author

I opened #34314 to unblock this PR.

@jeremiedbb jeremiedbb moved this to PR Stalled in Labs Jun 29, 2026
@StefanieSenger StefanieSenger moved this from PR Stalled to In progress in Labs Jun 29, 2026
@ogrisel ogrisel moved this from In progress to PR Stalled in Labs Jul 6, 2026
X, y, **routed_params.estimator.transform
)
return X

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.

Is it normal to return X?


def transform(self, X, y=None, **transform_params):
routed_params = process_routing(self, "transform", **transform_params)
self.estimator_ = clone(self.estimator).transform(

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.

For my understanding, could you tell my why it's necessary to clone the estimator?
I thought that estimator_ always was a fitted estimator, and transform needs a fitted estimator. No?

This branch has not been deployed

No deployments
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:utils

Projects

Status: To Review

Development

Successfully merging this pull request may close these issues.

4 participants