Repository navigation
Change all relative imports to absolute imports (except same-module imports) - #8200
Conversation
Codecov Report
@@ Coverage Diff @@
## master #8200 +/- ##
=======================================
Coverage 86.91% 86.91%
=======================================
Files 383 383
Lines 57889 57889
Branches 1056 1056
=======================================
Hits 50313 50313
Misses 6962 6962
Partials 614 614
Continue to review full report at Codecov.
|
|
If this is a go ahead (for which I'm 👍), could the same PR also be used for the import reordering #7827 given that both would cause disruption everywhere... |
|
(and I'm happy to sprint on the meeting to cleanup possible conflicts caused by this in other PRs during the meeting) |
|
I was skeptical of this, because to me relative is more readable, makes it easier to move things, and catches some import problems... But out-of-band, @astrofrog and @taldcroft (and a few others) were persuasive and convinced me it's a good idea. Because 1) Empirical evidence + PEP8 convinces me that I am the outlier in finding it more readable 2) it's not that common to move things and a search-and-replace does the job anyway, and 3) the py3.x absolute import business addresses most of those. So in the end I'm 👍 |
|
@astrofrog - rebase than and merge away as soon as the release is out to minimize conflicts. |
95c259b to
ec96566
Compare
|
Very much in favour! I did not look in great detail, but a suggestion in #6452 by @taldcroft was to have the in-module imports sit below the other astropy imports, i.e., effectively have four levels: python, other=numpy, other-astropy, local-to-module. Would the script be able to handle that? I see that, e.g., for |
|
I usually prefer local imports as it allows to easily see them, but here given the number of astropy imports at an higher level I agree that using absolute import is better. Concerning sorting imports, if we are there I think that it should be enforced with something like isort, to be sure that it remains consistent in the future. If there is an isort config then people can just configure their editor and use it. |
|
I wrote a isort config here: 88b423e |
|
It would be even nicer if there was a tool that automatically applied things to PRs, so that we avoid scaring new contributors away with style issues. |
|
I just sorted the imports now but I'd like to open a separate PR, as we need to review things a bit more carefully (there are places where the imports are in a specific order for a reason). So let's merge this first, then I'll open a follow-up. |
|
@astrofrog - maybe that follow-up can also be done in chuckes per subpackage or a few subpackages per PR? |
|
tl;dr -- Does developer doc need updating to reflect this new preference? |
|
@pllim - see the last changed file (you need to scroll down a lot!) |
pllim
left a comment
There was a problem hiding this comment.
I am no objection and seems like others agree as well.
0d5fdc6 to
377babc
Compare
eteq
left a comment
There was a problem hiding this comment.
This is fine except for one thing (which maybe we'll just blow past?) - some of these replacements now make the lines exceed 80 chars. Of course we're a bit lax about this, but it will look kinda weird in places where the (...) trick is used to meet the 80 char limit?
| as opposed to absolute (as PEP8 suggests) or the simpler ``import modname`` | ||
| syntax. This is primarily due to improved relative import support since PEP8 | ||
| was developed, and to simplify the process of moving modules. | ||
| * One exception is to be made from the PEP8 style: while absolute imports |
There was a problem hiding this comment.
This isn't really an exception, right? PEP8 says
However, explicit relative imports are an acceptable alternative to absolute imports, especially when dealing with complex package layouts where using absolute imports would be unnecessarily verbose
I'll make a PR suggesting a re-word (astrofrog#81)
|
@eteq - regarding the formatting of the imports, I'll fix that when reordering the imports in a separate PR. |
Absolute imports are generally more readable than e.g. 'from ...utils import x' and are recommended by PEP8. However, we keep imports from the same level (e.g. from .utils import ...) as relative imports.
b79e03f to
f749214
Compare
|
🎆 🎉 |
Change all relative imports to absolute imports (except same-module imports)
Change all relative imports to absolute imports (except same-module imports)
Absolute imports are generally more readable than e.g. 'from ...utils import x' and are recommended by PEP8. However, this keep imports from the same level (e.g. from .utils import ...) as relative imports. See #6452 for a discussion of this.
Fixes #6452