Move variable __gmt_version__ to pygmt.clib to avoid cyclic-import errors#2713
Move variable __gmt_version__ to pygmt.clib to avoid cyclic-import errors#2713
Conversation
| 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"] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Description of proposed changes
Defining
__gmt_version__inpygmt/__init__.pycauses cyclic-import errors.This PR fixes the issue by moving
__gmt_version__frompygmt/__init__.pyto
pygmt/clib/__init__.py.Closes #2712.
Reminders
make formatandmake checkto make sure the code follows the style guide.doc/api/index.rst.Slash Commands
You can write slash commands (
/command) in the first line of a comment to performspecific operations. Supported slash commands are:
/format: automatically format and lint the code/test-gmt-dev: run full tests on the latest GMT development version