Skip to content

Change all relative imports to absolute imports (except same-module imports) - #8200

Merged
eteq merged 3 commits into
astropy:masterfrom
astrofrog:absolute-imports
Dec 7, 2018
Merged

eteq merged 3 commits into
astropy:masterfrom
astrofrog:absolute-imports

Conversation

@astrofrog

@astrofrog astrofrog commented Nov 30, 2018 •

Copy link
Copy Markdown
Member

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

@codecov

codecov Bot commented Nov 30, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #8200 into master will not change coverage.
The diff coverage is 99.15%.

Impacted file tree graph

@@           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
Impacted Files Coverage Δ
astropy/coordinates/builtin_frames/gcrs.py 100% <100%> (ø) ⬆️
astropy/samp/errors.py 100% <100%> (ø) ⬆️
...dinates/builtin_frames/supergalactic_transforms.py 100% <100%> (ø) ⬆️
astropy/io/fits/connect.py 92.02% <100%> (ø) ⬆️
astropy/coordinates/distances.py 100% <100%> (ø) ⬆️
astropy/io/misc/hdf5.py 91.61% <100%> (ø) ⬆️
astropy/modeling/parameters.py 89.11% <100%> (ø) ⬆️
astropy/convolution/core.py 99.17% <100%> (ø) ⬆️
astropy/coordinates/builtin_frames/altaz.py 92.59% <100%> (ø) ⬆️
astropy/coordinates/builtin_frames/fk5.py 100% <100%> (ø) ⬆️
... and 136 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 644bad5...f749214. Read the comment docs.

@bsipocz

bsipocz commented Nov 30, 2018

Copy link
Copy Markdown
Member

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

@bsipocz

bsipocz commented Nov 30, 2018

Copy link
Copy Markdown
Member

(and I'm happy to sprint on the meeting to cleanup possible conflicts caused by this in other PRs during the meeting)

@eteq

eteq commented Dec 5, 2018

Copy link
Copy Markdown
Member

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 👍

@bsipocz

bsipocz commented Dec 5, 2018

Copy link
Copy Markdown
Member

@astrofrog - rebase than and merge away as soon as the release is out to minimize conflicts.

@pllim pllim removed the Experimental label Dec 5, 2018
@pllim pllim added this to the v3.2 milestone Dec 5, 2018
@mhvk

mhvk commented Dec 5, 2018

Copy link
Copy Markdown
Contributor

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 table, this fourth level already exists, but for units it does not (only looked at quantity.py).

@astrofrog

Copy link
Copy Markdown
Member Author

@mhvk - yes, @bsipocz also suggested reordering the imports. Since two of you now suggested that, I'll dig out my old script to do that...

@saimn

saimn commented Dec 5, 2018

Copy link
Copy Markdown
Contributor

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 had a Go training last week, and Go has a very constraining set of tools and formatting rules (and a tool to automatically insert or remove imports!). This is annoying when you have other preferences, but this is also a huge time saver. No need to think about the formatting style when it is enforced.

@Cadair

Cadair commented Dec 5, 2018

Copy link
Copy Markdown
Member

I wrote a isort config here: 88b423e

@mhvk

mhvk commented Dec 5, 2018

Copy link
Copy Markdown
Contributor

py-isort is in melpa, so it should be very easy indeed to use in emacs... nice!

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.

@astrofrog

Copy link
Copy Markdown
Member Author

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.

@bsipocz

bsipocz commented Dec 5, 2018

Copy link
Copy Markdown
Member

@astrofrog - maybe that follow-up can also be done in chuckes per subpackage or a few subpackages per PR?

@pllim

pllim commented Dec 5, 2018

Copy link
Copy Markdown
Member

tl;dr -- Does developer doc need updating to reflect this new preference?

@astrofrog

Copy link
Copy Markdown
Member Author

@pllim - see the last changed file (you need to scroll down a lot!)

@pllim pllim 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 am no objection and seems like others agree as well.

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

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?

Comment thread docs/development/codeguide.rst Outdated
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

@eteq eteq Dec 7, 2018 •

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.

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)

@astrofrog

Copy link
Copy Markdown
Member Author

@eteq - regarding the formatting of the imports, I'll fix that when reordering the imports in a separate PR.

astrofrog and others added 3 commits December 6, 2018 20:24
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.
@eteq
eteq merged commit de39336 into astropy:master Dec 7, 2018
@Cadair

Cadair commented Dec 7, 2018

Copy link
Copy Markdown
Member

🎆 🎉

@bsipocz bsipocz modified the milestones: v3.2, 3.1.1 Dec 7, 2018
bsipocz pushed a commit that referenced this pull request Dec 13, 2018
Change all relative imports to absolute imports (except same-module imports)
astrofrog pushed a commit to astropy/pytest-astropy-header that referenced this pull request Oct 7, 2019
Change all relative imports to absolute imports (except same-module imports)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants