Repository navigation
MNT Consistently use relative imports - #31817
Conversation
betatim
left a comment
There was a problem hiding this comment.
I don't know the history of the two testing files. Let's wait for someone who does.
Otherwise looks good to me.
It made me wonder if one of the tools we use for linting could help us here to prevent this happening again? It seems like ruff has 101 different rules about things, maybe one of them is "use relative imports within my package"?
|
Thanks for this, I actually noticed it as well the other day ... I somehow thought we had some kind of home-made linter to enforce our "relative imports except in test files" rule as per our guidelines but apparently not. Without very strong arguments, I slightly prefer keeping
More of a meta discussion for another day, but I have to admit I don't remember why we have the "use relative imports except in test files" rule ... |
just a convention I guess. And my other guess is that the choice was relative to keep import statements short-ish |
|
@lesteve shall we merge ? |
|
I was hoping someone would come in and enlighten us about a potential reason why Maybe @adrinjalali you have a suggestion, since you created Probably it's just accidental/historical but I figured I would ask ... |
|
I left the imports in the testing files as absolute to make sure nothing breaks when you start running things in the test suite / testing settings. However, as long as they're packaged inside One thing I'd say is that in the modern days, the good practice seems to be to have absolute imports everywhere, and I kinda prefer that. So I wouldn't oppose a PR converting them all to absolute imports, which looks much cleaner too. |
|
OK let's merge this first to be consistent with our own guidelines. We can have the discussion about relative vs absolute in a separate issue. For the record, I have a tiny preference for absolute imports:
I am still curious to hear about the original reasons for the choice of relative imports. |
|
I think the original reason was to make it possible to vendor/embed scikit-learn into another code-base, but given the fact that we had a mix of absolute and relative imports for a while and the fact that building compiled modules is complex, I am pretty sure that nobody does that so the use of relative imports is probably YAGNI case. I agree with Loïc that reading import statements adds a cumbersome reasoning indirection, both when editing (contributing to scikit-learn) but also when reading the code to understand the fine details of a given piece of code. |
Our convention is usually to use relative imports. During a review I noticed an absolute import within sklearn so I looked around and found a bunch of these throughout the code base.
I left absolute imports in
sklearn/conftest.pyfor now because I'm not sure what the impact of using relative imports there would be. Will check later.utils._testing.pyandutils.estimator_checkswere using only absolute imports. I believe it was so because we use absolute imports for the tests. But they are not test files, just tools in utils that we use in the tests. So I turned them into relative imports. I didn't notice any issue with that, but I might have missed something. Curious to know if there was a reason to use absolute imports there.