Skip to content

Commit 2dc3e6b

Browse files
authored
Use bool variable for importing optional modules (#2809)
1 parent 7aac7fd commit 2dc3e6b

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
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
@@ -107,7 +109,7 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret
107109
* y (y) float64 -7.081e-10 -7.858e+04 ... -1.996e+07 ...
108110
* x (x) float64 -2.004e+07 -1.996e+07 ... 1.996e+07 2.004e+07
109111
"""
110-
if contextily is None:
112+
if not _HAS_CONTEXTILY:
111113
raise ImportError(
112114
"Package `contextily` is required to be installed to use this function. "
113115
"Please use `python -m pip install contextily` or "

pygmt/figure.py

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

99
try:
1010
import IPython
11+
12+
_HAS_IPYTHON = True
1113
except ImportError:
12-
IPython = None
14+
_HAS_IPYTHON = False
15+
1316

1417
from pygmt.clib import Session
1518
from pygmt.exceptions import GMTError, GMTInvalidInput
@@ -32,7 +35,7 @@
3235
}
3336

3437
# Show figures in Jupyter notebooks if available
35-
if IPython:
38+
if _HAS_IPYTHON:
3639
get_ipython = IPython.get_ipython()
3740
if get_ipython and "IPKernelApp" in get_ipython.config: # Jupyter Notebook enabled
3841
SHOW_CONFIG["method"] = "notebook"
@@ -448,7 +451,7 @@ def show(self, dpi=300, width=500, method=None, waiting=0.5, **kwargs):
448451
)
449452

450453
if method == "notebook":
451-
if IPython is None:
454+
if not _HAS_IPYTHON:
452455
raise GMTError(
453456
"Notebook display is selected, but IPython is not available. "
454457
"Make sure you have IPython installed, "

pygmt/src/tilemap.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
77

88
try:
9-
import rioxarray
9+
import rioxarray # noqa: F401
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)
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@
33
44
Doesn't include the plotting commands which have their own test files.
55
"""
6+
import importlib
67
import os
78
from pathlib import Path
89

9-
try:
10-
import IPython
11-
except ImportError:
12-
IPython = None
13-
1410
import numpy as np
1511
import numpy.testing as npt
1612
import pytest
1713
from pygmt import Figure, set_display
1814
from pygmt.exceptions import GMTError, GMTInvalidInput
1915
from pygmt.helpers import GMTTempFile
2016

17+
HAS_IPYTHON = bool(importlib.util.find_spec("IPython"))
18+
2119

2220
def test_figure_region():
2321
"""
@@ -311,7 +309,7 @@ def test_figure_savefig_worldfile():
311309
fig.savefig(fname=imgfile.name, worldfile=True)
312310

313311

314-
@pytest.mark.skipif(IPython is None, reason="run when IPython is installed")
312+
@pytest.mark.skipif(not HAS_IPYTHON, reason="run when IPython is installed")
315313
def test_figure_show():
316314
"""
317315
Test that show creates the correct file name and deletes the temp dir.
@@ -352,7 +350,7 @@ def test_figure_show_invalid_method():
352350
fig.show(method="test")
353351

354352

355-
@pytest.mark.skipif(IPython is not None, reason="run without IPython installed")
353+
@pytest.mark.skipif(HAS_IPYTHON, reason="run without IPython installed")
356354
def test_figure_show_notebook_error_without_ipython():
357355
"""
358356
Test to check if an error is raised when display method is 'notebook', but

0 commit comments

Comments
 (0)