Skip to content

Commit 5deebf8

Browse files
committed
Use a bool variable to indicate whether an optional module is available
1 parent 6ae2072 commit 5deebf8

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

pygmt/datasets/tile_map.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
try:
77
import contextily
8+
9+
_has_contextily = True
810
except ImportError:
9-
contextily = None
11+
_has_contextily = False
1012

1113
import numpy as np
1214
import xarray as xr
@@ -108,7 +110,7 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret
108110
* x (x) float64 -2.004e+07 -1.996e+07 ... 1.996e+07 2.004e+07
109111
"""
110112
# pylint: disable=too-many-locals
111-
if contextily is None:
113+
if not _has_contextily:
112114
raise ImportError(
113115
"Package `contextily` is required to be installed to use this function. "
114116
"Please use `python -m pip install contextily` or "

pygmt/figure.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
try:
1010
import IPython
11+
12+
_has_ipython = True
1113
except ImportError:
12-
IPython = None # pylint: disable=invalid-name
14+
_has_ipython = False
1315

1416

1517
from pygmt.clib import Session
@@ -33,7 +35,7 @@
3335
}
3436

3537
# Show figures in Jupyter notebooks if available
36-
if IPython:
38+
if _has_ipython:
3739
get_ipython = IPython.get_ipython() # pylint: disable=invalid-name
3840
if get_ipython and "IPKernelApp" in get_ipython.config: # Jupyter Notebook enabled
3941
SHOW_CONFIG["method"] = "notebook"
@@ -452,7 +454,7 @@ def show(self, dpi=300, width=500, method=None, waiting=0.5, **kwargs):
452454
)
453455

454456
if method == "notebook":
455-
if IPython is None:
457+
if not _has_ipython:
456458
raise GMTError(
457459
"Notebook display is selected, but IPython is not available. "
458460
"Make sure you have IPython installed, "

pygmt/src/tilemap.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
try:
99
import rioxarray
10+
11+
_has_rioxarray = True
1012
except ImportError:
11-
rioxarray = None
13+
_has_rioxarray = False
1214

1315

1416
@fmt_docstring
@@ -114,7 +116,7 @@ def tilemap(
114116
"""
115117
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
116118

117-
if rioxarray is None:
119+
if not _has_rioxarray:
118120
raise ImportError(
119121
"Package `rioxarray` is required to be installed to use this function. "
120122
"Please use `python -m pip install rioxarray` or "

pygmt/tests/test_figure.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
try:
1010
import IPython
11+
12+
_has_ipython = True
1113
except ImportError:
12-
IPython = None # pylint: disable=invalid-name
14+
_has_ipython = False
1315

1416

1517
import numpy as np
@@ -313,7 +315,7 @@ def test_figure_savefig_worldfile():
313315
fig.savefig(fname=imgfile.name, worldfile=True)
314316

315317

316-
@pytest.mark.skipif(IPython is None, reason="run when IPython is installed")
318+
@pytest.mark.skipif(not _has_ipython, reason="run when IPython is installed")
317319
def test_figure_show():
318320
"""
319321
Test that show creates the correct file name and deletes the temp dir.
@@ -354,7 +356,7 @@ def test_figure_show_invalid_method():
354356
fig.show(method="test")
355357

356358

357-
@pytest.mark.skipif(IPython is not None, reason="run without IPython installed")
359+
@pytest.mark.skipif(_has_ipython, reason="run without IPython installed")
358360
def test_figure_show_notebook_error_without_ipython():
359361
"""
360362
Test to check if an error is raised when display method is 'notebook', but

0 commit comments

Comments
 (0)