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

Move variable __gmt_version__ to pygmt.clib to avoid cyclic-import errors #2713

Merged
merged 2 commits into from Oct 5, 2023

Conversation

seisman
Copy link
Member

@seisman seisman commented Oct 3, 2023

Description of proposed changes

Defining __gmt_version__ in pygmt/__init__.py causes cyclic-import errors.
This PR fixes the issue by moving __gmt_version__ from pygmt/__init__.py
to pygmt/clib/__init__.py.

Closes #2712.

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 wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.
  • Use underscores (not hyphens) in names of Python files and directories.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@seisman seisman added the maintenance Boring but important stuff for the core devs label Oct 3, 2023
@seisman seisman added this to the 0.11.0 milestone Oct 3, 2023
@seisman seisman added the needs review This PR has higher priority and needs review. label Oct 3, 2023
Comment on lines -24 to -30
from pygmt import clib

# Get semantic version through setuptools-scm
__version__ = f'v{version("pygmt")}' # e.g. v0.1.2.dev3+g0ab3cd78
__commit__ = __version__.split("+g")[-1] if "+g" in __version__ else "" # 0ab3cd78
with clib.Session() as lib:
__gmt_version__ = lib.info["version"]
Copy link
Member

@weiji14 weiji14 Oct 3, 2023

Choose a reason for hiding this comment

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

Looking at how rasterio handles __gdal_version__ at https://github.com/rasterio/rasterio/blob/6bf045cc5e02c9f28e78d48b97b308490207d27d/rasterio/__init__.py#L84-L86, they do:

from rasterio._version import gdal_version

__gdal_version__ = gdal_version()

They have a _version.pyx and a _version.pxd file that seems to be reading from the gdal.h header file. Do you think we could implement something similar for PyGMT/GMT by looking at include/gmt_version.h?

Copy link
Member Author

Choose a reason for hiding this comment

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

The way we get the GMT version string doesn't matter. What matters is that the __gmt_version__ variable is defined in pygmt/__init__.py and is internally used in other files.

Take pygmt/src/timestamp.py as an example. It imports the __gmt_version__ variable from pygmt/__init__.py. However, pygmt/__init__.py imports Figure from pygmt/figure.py and pygmt/figure.py imports timestamp from pygmt/src/timestamp.py. Thus, there are always cyclic import issues if we define __gmt_version__ in pygmt/__init__.py.

The only solution I can see is defining __gmt_version__ in another file, like pygmt/clib/__init__.py in this PR, or a new file like pygmt/_version.py.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, let's keep things in pygmt/clib/__init__.py for now since it's all internal variables, to get the Style Checks passing. We can look into moving things later to _version.py if needed.

@seisman seisman merged commit 0705846 into main Oct 5, 2023
12 of 16 checks passed
@seisman seisman deleted the cyclic-import branch October 5, 2023 05:03
@seisman seisman removed the needs review This PR has higher priority and needs review. label Oct 5, 2023
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.

pylint 3.0.0 reports cyclic-import errors
2 participants