Skip to content
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

Convert relative imports to absolute imports #754

Merged
merged 4 commits into from Dec 21, 2020
Merged

Convert relative imports to absolute imports #754

merged 4 commits into from Dec 21, 2020

Conversation

seisman
Copy link
Member

@seisman seisman commented Dec 19, 2020

Description of proposed changes

This PR converts all relative imports to absolute imports.

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If adding new functionality, add an example to docstrings or tutorials.

Notes

  • You can write /format in the first line of a comment to lint the code automatically

@seisman seisman added the maintenance Boring but important stuff for the core devs label Dec 19, 2020
@seisman seisman added this to the 0.3.0 milestone Dec 19, 2020
@seisman seisman added the skip-changelog Skip adding Pull Request to changelog label Dec 19, 2020
@seisman seisman requested a review from a team December 19, 2020 18:06
@seisman seisman changed the title test: Improve from local folder test: Import from local folder Dec 19, 2020
from pygmt.helpers import GMTTempFile
from ..helpers import GMTTempFile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're on this. Do we want to actually want to continue using relative imports or switch to absolute imports? From https://realpython.com/absolute-vs-relative-python-imports/#pros-and-cons-of-absolute-imports:

Absolute imports are preferred because they are quite clear and straightforward. It is easy to tell exactly where the imported resource is, just by looking at the statement. Additionally, absolute imports remain valid even if the current location of the import statement changes. In fact, PEP 8 explicitly recommends absolute imports.

One clear advantage of relative imports is that they are quite succinct. Depending on the current location, they can turn the ridiculously long import statement you saw earlier to something as simple as this:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. PyGMT doesn't have a deep folder structure, so it seems absolute imports are better. I'm OK with changing relative imports to absolute imports.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know if you want me to convert relative imports to absolute imports in this PR.

Copy link
Member

@weiji14 weiji14 Dec 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I much prefer absolute imports actually. It'll also help a bit with #686. I think it's fine to do it here in this PR.

And just for context, I think the relative imports were from the old days of gmt-python. It's easier to change the package name with relative imports, but I don't think we'll change pygmt's name anymore now that it's more established.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know any tools which can do the conversion automatically?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. Probably just regular search and replace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before that, I think we should let pylint check and warn relative imports.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to add either W0403 or relative-import to disable= or enable= in .pylintrc, but none of them warn about relative imports. Am I misunderstanding how pylint works?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that we have no-absolute-import disabled, does that do anything? Actually have no clue how to set this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not related:

no-absolute-import (W1618):

import missing from __future__ import absolute_import Used when an import is not accompanied by from future import absolute_import (default behaviour in Python 3)

@seisman seisman marked this pull request as draft December 20, 2020 06:16
@seisman seisman changed the title test: Import from local folder Convert relative imports to absolute imports Dec 20, 2020
@weiji14 weiji14 removed the skip-changelog Skip adding Pull Request to changelog label Dec 20, 2020
@seisman
Copy link
Member Author

seisman commented Dec 21, 2020

This PR is almost done, but we have this error:

pygmt/__init__.py:43:4: C0415: Import outside toplevel (pygmt.clib.Session) (import-outside-toplevel)

@weiji14 Do you have any idea what it means?

@seisman seisman marked this pull request as ready for review December 21, 2020 22:51
Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@seisman seisman merged commit 6c7a673 into master Dec 21, 2020
@seisman seisman deleted the fix-meca-test branch December 21, 2020 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants