Skip to content

MNT Consistently use relative imports - #31817

Merged
lesteve merged 2 commits into
scikit-learn:mainfrom
jeremiedbb:consistent_relative_imports
Jul 28, 2025
Merged

lesteve merged 2 commits into
scikit-learn:mainfrom
jeremiedbb:consistent_relative_imports

Conversation

@jeremiedbb

@jeremiedbb jeremiedbb commented Jul 22, 2025 •

Copy link
Copy Markdown
Member

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.py for now because I'm not sure what the impact of using relative imports there would be. Will check later.

utils._testing.py and utils.estimator_checks were 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.

@github-actions

Copy link
Copy Markdown

✔️ Linting Passed

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

Generated for commit: bb98d1c. Link to the linter CI: here

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

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"?

@lesteve

lesteve commented Jul 23, 2025 •

Copy link
Copy Markdown
Member

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 sklearn/conftest.py with absolute imports:

  • sklearn/conftest.py in my mind is a way to tweak pytest behaviour and not really part of the sklearn package (hopefully not many people do import sklearn.conftest ...)
  • I have found it convenient to debug weird pytest behaviour to copy sklearn/conftest.py to a new scratch project without the need to change conftest.py.
  • doc/conftest.py can not use relative imports so this would introduce a small inconsistency between sklearn/conftest.py and doc/conftest.py

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

@jeremiedbb

Copy link
Copy Markdown
Member Author

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

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

LGTM! Thanks @jeremiedbb!

@jeremiedbb

Copy link
Copy Markdown
Member Author

@lesteve shall we merge ?

@lesteve

lesteve commented Jul 28, 2025

Copy link
Copy Markdown
Member

I was hoping someone would come in and enlighten us about a potential reason why sklearn.utils._testing and skearn.utils._test_common were using absolute imports.

Maybe @adrinjalali you have a suggestion, since you created sklearn.utils._test_common 11 months ago?

Probably it's just accidental/historical but I figured I would ask ...

@adrinjalali

Copy link
Copy Markdown
Member

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 sklearn/, it doesn't really matter.

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.

@lesteve

lesteve commented Jul 28, 2025

Copy link
Copy Markdown
Member

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:

  • this seems enforceable by ruff contrary to our own guideline, see TID252 rule and ban-relative-imports doc
  • having to count the number of dots is slightly cumbersome from ...base import bla

I am still curious to hear about the original reasons for the choice of relative imports.

@lesteve
lesteve merged commit 4abf564 into scikit-learn:main Jul 28, 2025
@ogrisel

ogrisel commented Jul 28, 2025

Copy link
Copy Markdown
Member

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.

lucyleeow pushed a commit to lucyleeow/scikit-learn that referenced this pull request Aug 22, 2025
@jeremiedbb jeremiedbb mentioned this pull request Sep 3, 2025
13 tasks done
@jeremiedbb
jeremiedbb deleted the consistent_relative_imports branch September 3, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants