Skip to content

Commit 419a080

Browse files
authored
Refactor the _get_module_version function using importlib and add two more tests (#3538)
1 parent 29f12f5 commit 419a080

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

pygmt/_show_versions.py

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
"""
2-
Utility methods to print system info for debugging.
2+
Utility methods to print system information for debugging.
33
44
Adapted from :func:`rioxarray.show_versions` and :func:`pandas.show_versions`.
55
"""
66

7-
import importlib
87
import platform
98
import shutil
109
import subprocess
1110
import sys
12-
from importlib.metadata import version
11+
from importlib.metadata import PackageNotFoundError, requires, version
1312
from typing import TextIO
1413

1514
from packaging.requirements import Requirement
@@ -25,25 +24,17 @@ def _get_clib_info() -> dict[str, str]:
2524
"""
2625
Get information about the GMT shared library.
2726
"""
28-
with Session() as ses:
29-
return ses.info
27+
with Session() as lib:
28+
return lib.info
3029

3130

3231
def _get_module_version(modname: str) -> str | None:
3332
"""
3433
Get version information of a Python module.
3534
"""
3635
try:
37-
if modname in sys.modules:
38-
module = sys.modules[modname]
39-
else:
40-
module = importlib.import_module(modname)
41-
42-
try:
43-
return module.__version__
44-
except AttributeError:
45-
return module.version
46-
except ImportError:
36+
return version(modname)
37+
except PackageNotFoundError:
4738
return None
4839

4940

@@ -85,13 +76,12 @@ def _check_ghostscript_version(gs_version: str | None) -> str | None:
8576
f"Ghostscript v{gs_version} has known bugs. "
8677
"Please consider upgrading to version v10.02 or later."
8778
)
88-
case v if v >= Version("10.02"):
89-
if Version(__gmt_version__) < Version("6.5.0"):
90-
return (
91-
f"GMT v{__gmt_version__} doesn't support Ghostscript "
92-
f"v{gs_version}. Please consider upgrading to GMT>=6.5.0 or "
93-
"downgrading to Ghostscript v9.56."
94-
)
79+
case v if v >= Version("10.02") and Version(__gmt_version__) < Version("6.5.0"):
80+
return (
81+
f"GMT v{__gmt_version__} doesn't support Ghostscript v{gs_version}. "
82+
"Please consider upgrading to GMT>=6.5.0 or downgrading to Ghostscript "
83+
"v9.56."
84+
)
9585
return None
9686

9787

@@ -109,22 +99,14 @@ def show_versions(file: TextIO | None = sys.stdout):
10999
It also warns users if the installed Ghostscript version has serious bugs or is
110100
incompatible with the installed GMT version.
111101
"""
112-
113102
sys_info = {
114103
"python": sys.version.replace("\n", " "),
115104
"executable": sys.executable,
116105
"machine": platform.platform(),
117106
}
118-
dep_info = {
119-
Requirement(v).name: _get_module_version(Requirement(v).name)
120-
for v in importlib.metadata.requires("pygmt") # type: ignore[union-attr]
121-
}
122-
dep_info.update(
123-
{
124-
"gdal": _get_module_version("osgeo.gdal"),
125-
"ghostscript": _get_ghostscript_version(),
126-
}
127-
)
107+
requirements = [Requirement(v).name for v in requires("pygmt")] + ["gdal"] # type: ignore[union-attr]
108+
dep_info = {name: _get_module_version(name) for name in requirements}
109+
dep_info.update({"ghostscript": _get_ghostscript_version()})
128110

129111
lines = []
130112
lines.append("PyGMT information:")

pygmt/tests/test_show_versions.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import io
6+
from unittest.mock import patch
67

78
import pygmt
89
import pytest
@@ -33,15 +34,38 @@ def test_show_versions():
3334
(None, "6.5.0"),
3435
],
3536
)
36-
def test_show_versions_ghostscript_warnings(gs_version, gmt_version, monkeypatch):
37+
def test_show_versions_ghostscript_warnings(gs_version, gmt_version):
3738
"""
3839
Check that pygmt.show_versions reports warnings for GMT-Ghostscript incompatibility.
3940
"""
40-
monkeypatch.setattr("pygmt._show_versions.__gmt_version__", gmt_version)
41-
monkeypatch.setattr(
42-
"pygmt._show_versions._get_ghostscript_version", lambda: gs_version
43-
)
41+
with (
42+
patch("pygmt._show_versions.__gmt_version__", gmt_version),
43+
patch("pygmt._show_versions._get_ghostscript_version", return_value=gs_version),
44+
):
45+
buf = io.StringIO()
46+
pygmt.show_versions(file=buf)
47+
assert "WARNING:" in buf.getvalue()
4448

45-
buf = io.StringIO()
46-
pygmt.show_versions(file=buf)
47-
assert "WARNING:" in buf.getvalue()
49+
50+
def test_show_versions_ghostscript_unsupported_os():
51+
"""
52+
Check that pygmt.show_versions reports ghostscript version is None for an
53+
unsupported operating system.
54+
"""
55+
with patch("sys.platform", new="unsupported_os"):
56+
buf = io.StringIO()
57+
pygmt.show_versions(file=buf)
58+
assert "ghostscript: None" in buf.getvalue()
59+
assert "WARNING:" in buf.getvalue()
60+
61+
62+
def test_show_versions_ghostscript_not_found():
63+
"""
64+
Check that pygmt.show_versions reports ghostscript version is None when ghostscript
65+
is not found in the system.
66+
"""
67+
with patch("shutil.which", return_value=None):
68+
buf = io.StringIO()
69+
pygmt.show_versions(file=buf)
70+
assert "ghostscript: None" in buf.getvalue()
71+
assert "WARNING:" in buf.getvalue()

0 commit comments

Comments
 (0)