Skip to content

MNT little refactor and doc improvement for metadata routing consumes() methods #31703

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 5 commits into from
Jul 11, 2025

Conversation

StefanieSenger
Copy link
Member

@StefanieSenger StefanieSenger commented Jul 4, 2025

What does this implement/fix? Explain your changes.

This PR aims to make metadata routing more intuitive and maintainable by a little refactoring and doc improvements:

  • simplifies the code in MetadataRequest.consumes() to use set notation (which I think is easier to read)
  • improves the docstrings of all three consumes methods
  • renames variables names to make them more specific
  • corrects a mistake in test_metadata_request_consumes_method

@StefanieSenger StefanieSenger added No Changelog Needed Metadata Routing all issues related to metadata routing, slep006, sample props labels Jul 4, 2025
Copy link

github-actions bot commented Jul 4, 2025

✔️ Linting Passed

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

Generated for commit: 7c849ce. Link to the linter CI: here


Returns
-------
consumed : set of str
A set of parameters which are consumed by the given method.
method_consumes : set of str
Copy link
Member Author

Choose a reason for hiding this comment

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

Also renaming consumed into method_consumes to prevent the expectation of a boolean.

It could also be consumes?

Copy link
Member

Choose a reason for hiding this comment

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

consumed_params ?

Copy link
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 that reads well. :)

Comment on lines 934 to 936
method_consumes.update(
route_mapping.router.consumes(method=callee, params=params)
)
Copy link
Member Author

@StefanieSenger StefanieSenger Jul 4, 2025

Choose a reason for hiding this comment

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

I've replaced res = res | ... with res.update(...) for readability.

The bitwise OR feels like a lower-level tool and using the set notation instead makes it easier for anyone reading this and they are not required to interpret a bitwise operator.

Do you agree with that, @adrinjalali?

Copy link
Member

@adrinjalali adrinjalali Jul 7, 2025

Choose a reason for hiding this comment

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

I 💯 disagree and I cringe when I see the nice one-liner being made a long method call and spanning 3 lines. But I know @glemaitre also prefers your way. I think I've lost that battle 😅 😆 . If y'all think this version reads better, then that's what's important. I'll keep my operators to my own code.

To me personally, with this longer variable names and removing operators, the code reads less easy than before, but I can still read it and live with it.

Copy link
Member Author

@StefanieSenger StefanieSenger Jul 7, 2025

Choose a reason for hiding this comment

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

It is just a proposal and I'm fine with leaving it as is, too.

reasoning:
I have now learned that we can operate on a bit-wise level on immutable objects. I was thinking that might be an additional stretch we could spare others to do. I would prefer short variable names too if they are expressive (but res?) though I find this is hard to find often.
Also, it was already a three-liner before, so in that sense, the change maybe isn't too bad?

meta talk:
I really value you recognising my need for explicitness. ❤️ I don't want this to feel like a battle at all.

Edit: Ah true, above this change turns a one-liner into a three-liner, unfortunately.

Copy link
Member

Choose a reason for hiding this comment

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

I'm okay to keep the code as you have it here. But res is quite a common and (to me at least) a very self explanatory name. You can grep for res = in the codebase and see many other places with the same pattern. It saves space and it's clear what it is.

@@ -571,22 +572,27 @@ def __init__(self, owner):
)

def consumes(self, method, params):
"""Check whether the given metadata are consumed by the given method.
"""Return params consumed as metadata in a :term:`consumer`.
Copy link
Member Author

@StefanieSenger StefanieSenger Jul 4, 2025

Choose a reason for hiding this comment

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

Several times it happened to me that at first sight I expected consumes() would return a boolean. Now this summary line hopefully prevents contributors and developers (and myself) to fall into this trap again.

@@ -638,14 +638,14 @@ class Consumer(BaseEstimator):
@config_context(enable_metadata_routing=True)
def test_metadata_request_consumes_method():
"""Test that MetadataRequest().consumes() method works as expected."""
request = MetadataRouter(owner="test")
request = MetadataRequest(owner="test_method")
Copy link
Member Author

@StefanieSenger StefanieSenger Jul 4, 2025

Choose a reason for hiding this comment

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

Corrected wrong class name.

@StefanieSenger
Copy link
Member Author

That's ready for a review, @adrinjalali. Would you have a look?

Copy link
Member

@lucyleeow lucyleeow left a comment

Choose a reason for hiding this comment

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

I like the changes, only some nitpicks on wording


Returns
-------
consumed : set of str
A set of parameters which are consumed by the given method.
method_consumes : set of str
Copy link
Member

Choose a reason for hiding this comment

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

consumed_params ?

assert request.consumes(method="fit", params={"foo"}) == set()

request = MetadataRequest(owner="test")
request = MetadataRequest(owner="test_method")
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why the diff here. The owner in the whole test file is mostly "test", and it doesn't really matter what it is. I don't mind it either way, but it's not something I'd go out of my way to change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, I have reverted this change.

My intend had been to make this test more intuitive to read for future contributors or third party developers, but I see my change was not quite correct and and I should have done it in all the places at the same time anyways.

Comment on lines 934 to 936
method_consumes.update(
route_mapping.router.consumes(method=callee, params=params)
)
Copy link
Member

@adrinjalali adrinjalali Jul 7, 2025

Choose a reason for hiding this comment

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

I 💯 disagree and I cringe when I see the nice one-liner being made a long method call and spanning 3 lines. But I know @glemaitre also prefers your way. I think I've lost that battle 😅 😆 . If y'all think this version reads better, then that's what's important. I'll keep my operators to my own code.

To me personally, with this longer variable names and removing operators, the code reads less easy than before, but I can still read it and live with it.

StefanieSenger and others added 2 commits July 7, 2025 11:08
Co-authored-by: Lucy Liu <jliu176@gmail.com>
Copy link
Member Author

@StefanieSenger StefanieSenger left a comment

Choose a reason for hiding this comment

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

Thank you two for your fast reviews, @lucyleeow and @adrinjalali! I have made a few updates that you had suggested and on the question of bit-wise comparison or set notation, I am open to change back and will wait for where the discussion goes.

assert request.consumes(method="fit", params={"foo"}) == set()

request = MetadataRequest(owner="test")
request = MetadataRequest(owner="test_method")
Copy link
Member Author

Choose a reason for hiding this comment

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

Right, I have reverted this change.

My intend had been to make this test more intuitive to read for future contributors or third party developers, but I see my change was not quite correct and and I should have done it in all the places at the same time anyways.


Returns
-------
consumed : set of str
A set of parameters which are consumed by the given method.
method_consumes : set of str
Copy link
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 that reads well. :)

Comment on lines 934 to 936
method_consumes.update(
route_mapping.router.consumes(method=callee, params=params)
)
Copy link
Member Author

@StefanieSenger StefanieSenger Jul 7, 2025

Choose a reason for hiding this comment

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

It is just a proposal and I'm fine with leaving it as is, too.

reasoning:
I have now learned that we can operate on a bit-wise level on immutable objects. I was thinking that might be an additional stretch we could spare others to do. I would prefer short variable names too if they are expressive (but res?) though I find this is hard to find often.
Also, it was already a three-liner before, so in that sense, the change maybe isn't too bad?

meta talk:
I really value you recognising my need for explicitness. ❤️ I don't want this to feel like a battle at all.

Edit: Ah true, above this change turns a one-liner into a three-liner, unfortunately.

Copy link
Member

@lucyleeow lucyleeow left a comment

Choose a reason for hiding this comment

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

LGTM thanks!

@lucyleeow lucyleeow merged commit 4206d14 into scikit-learn:main Jul 11, 2025
36 checks passed
@jeremiedbb jeremiedbb mentioned this pull request Jul 15, 2025
13 tasks
@StefanieSenger StefanieSenger deleted the metadata_routing_consumes branch July 18, 2025 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Metadata Routing all issues related to metadata routing, slep006, sample props module:utils No Changelog Needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants