Skip to content

Conversation

@seisman
Copy link
Member

@seisman seisman commented Oct 19, 2024

The private _get_module_version function was initially added 4 years ago in PR #466. At that time, we supported Python 3.6-3.8 and importlib.metadata was not available yet.

importlib.metadata was added in Python 3.8. With this module, getting package version can be greatly simplified.

This PR simplifies the _get_module_version function and also add two more tests related to Ghostscript to increase code coverage by 0.16% (now we reach 98.49%).

Without optional packages:

PyGMT information:
  version: v0.13.1.dev94+ge7c91571
System information:
  python: 3.10.15 | packaged by conda-forge | (main, Sep 30 2024, 17:51:04) [GCC 13.3.0]
  executable: /home/runner/micromamba/envs/pygmt/bin/python
  machine: Linux-6.5.0-1025-azure-x86_64-with-glibc2.35
Dependency information:
  numpy: 1.24.4
  pandas: 2.0.3
  xarray: 2023.4.2
  netCDF4: 1.7.1
  packaging: 24.1
  contextily: None
  geopandas: None
  IPython: None
  rioxarray: None
  gdal: 3.9.2
  ghostscript: 10.04.0
GMT library information:
  version: 6.5.0
  padding: 2
  share dir: /home/runner/micromamba/envs/pygmt/share/gmt
  plugin dir: /home/runner/micromamba/envs/pygmt/lib/gmt/plugins
  library path: /home/runner/micromamba/envs/pygmt/lib/libgmt.so
  cores: 4
  grid layout: rows
  image layout: 
  binary version: 6.5.0

With optional packages:

PyGMT information:
  version: v0.13.1.dev94+ge7c91571
System information:
  python: 3.12.7 | packaged by conda-forge | (main, Oct  4 2024, 16:05:46) [GCC 13.3.0]
  executable: /home/runner/micromamba/envs/pygmt/bin/python
  machine: Linux-6.5.0-1025-azure-x86_64-with-glibc2.35
Dependency information:
  numpy: 2.1.2
  pandas: 2.2.3
  xarray: 2024.9.0
  netCDF4: 1.7.1
  packaging: 24.1
  contextily: 1.6.2
  geopandas: 1.0.1
  IPython: 8.28.0
  rioxarray: 0.17.0
  gdal: 3.9.2
  ghostscript: 10.04.0
GMT library information:
  version: 6.5.0
  padding: 2
  share dir: /home/runner/micromamba/envs/pygmt/share/gmt
  plugin dir: /home/runner/micromamba/envs/pygmt/lib/gmt/plugins
  library path: /home/runner/micromamba/envs/pygmt/lib/libgmt.so
  cores: 4
  grid layout: rows
  image layout: 
  binary version: 6.5.0

@seisman seisman added maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog needs review This PR has higher priority and needs review. labels Oct 19, 2024
@seisman seisman added this to the 0.14.0 milestone Oct 19, 2024
@seisman seisman force-pushed the refactor/show_versions branch 2 times, most recently from 8d02283 to c6b3c70 Compare October 19, 2024 10:40
"ghostscript": _get_ghostscript_version(),
}
)
requirements = [Requirement(v).name for v in requires("pygmt")] + ["gdal"] # type: ignore[union-attr]
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously, we have to use _get_module_version("osgeo.gdal"), now we can use _get_module_version("gdal").

Copy link
Member

Choose a reason for hiding this comment

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

Nice to see that importlib.metadata.version("gdal") works!

@seisman seisman force-pushed the refactor/show_versions branch from c6b3c70 to 86daafb Compare October 19, 2024 10:47
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.

Thanks @seisman, I'm a little unsure of whether to use pytest.monkeypatch or mock, but do think mock can look more intuitive as you've shown here. Can discuss more in #3537.

"ghostscript": _get_ghostscript_version(),
}
)
requirements = [Requirement(v).name for v in requires("pygmt")] + ["gdal"] # type: ignore[union-attr]
Copy link
Member

Choose a reason for hiding this comment

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

Nice to see that importlib.metadata.version("gdal") works!

@seisman seisman removed needs review This PR has higher priority and needs review. skip-changelog Skip adding Pull Request to changelog labels Oct 21, 2024
@seisman seisman changed the title Simplify the _get_module_version function and add two more tests Refactor the _get_module_version function using importlib and add two more tests Oct 21, 2024
@seisman seisman merged commit 419a080 into main Oct 21, 2024
@seisman seisman deleted the refactor/show_versions branch October 21, 2024 06: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.

4 participants