-
-
Notifications
You must be signed in to change notification settings - Fork 26.2k
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
MNT little refactor and doc improvement for metadata routing consumes() methods #31703
Conversation
sklearn/utils/_metadata_requests.py
Outdated
|
||
Returns | ||
------- | ||
consumed : set of str | ||
A set of parameters which are consumed by the given method. | ||
method_consumes : set of str |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consumed_params
?
There was a problem hiding this comment.
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. :)
sklearn/utils/_metadata_requests.py
Outdated
method_consumes.update( | ||
route_mapping.router.consumes(method=callee, params=params) | ||
) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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`. |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected wrong class name.
That's ready for a review, @adrinjalali. Would you have a look? |
There was a problem hiding this 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
sklearn/utils/_metadata_requests.py
Outdated
|
||
Returns | ||
------- | ||
consumed : set of str | ||
A set of parameters which are consumed by the given method. | ||
method_consumes : set of str |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
sklearn/utils/_metadata_requests.py
Outdated
method_consumes.update( | ||
route_mapping.router.consumes(method=callee, params=params) | ||
) |
There was a problem hiding this comment.
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.
Co-authored-by: Lucy Liu <jliu176@gmail.com>
There was a problem hiding this 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") |
There was a problem hiding this comment.
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.
sklearn/utils/_metadata_requests.py
Outdated
|
||
Returns | ||
------- | ||
consumed : set of str | ||
A set of parameters which are consumed by the given method. | ||
method_consumes : set of str |
There was a problem hiding this comment.
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. :)
sklearn/utils/_metadata_requests.py
Outdated
method_consumes.update( | ||
route_mapping.router.consumes(method=callee, params=params) | ||
) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks!
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:
MetadataRequest.consumes()
to use set notation (which I think is easier to read)consumes
methodstest_metadata_request_consumes_method