Skip to content

Commit 44f9447

Browse files
committed
Figure.show: Raise ImportError instead of GMTError if IPython is not installed but required (#3580)
1 parent 82f811e commit 44f9447

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

pygmt/figure.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import numpy as np
1919
from pygmt.clib import Session
20-
from pygmt.exceptions import GMTError, GMTInvalidInput
20+
from pygmt.exceptions import GMTInvalidInput
2121
from pygmt.helpers import launch_external_viewer, unique_name
2222

2323

@@ -331,11 +331,12 @@ def show(
331331
match method:
332332
case "notebook":
333333
if not _HAS_IPYTHON:
334-
raise GMTError(
334+
msg = (
335335
"Notebook display is selected, but IPython is not available. "
336336
"Make sure you have IPython installed, "
337337
"or run the script in a Jupyter notebook."
338338
)
339+
raise ImportError(msg)
339340
png = self._preview(
340341
fmt="png", dpi=dpi, anti_alias=True, as_bytes=True, **kwargs
341342
)
@@ -344,14 +345,15 @@ def show(
344345
pdf = self._preview(
345346
fmt="pdf", dpi=dpi, anti_alias=False, as_bytes=False, **kwargs
346347
)
347-
launch_external_viewer(pdf, waiting=waiting) # type: ignore[arg-type]
348+
launch_external_viewer(pdf, waiting=waiting)
348349
case "none":
349350
pass # Do nothing
350351
case _:
351-
raise GMTInvalidInput(
352-
f"Invalid display method '{method}'. Valid values are 'external', "
353-
"'notebook', 'none' or None."
352+
msg = (
353+
f"Invalid display method '{method}'. "
354+
"Valid values are 'external', 'notebook', 'none' or None."
354355
)
356+
raise GMTInvalidInput(msg)
355357

356358
def _preview(self, fmt: str, dpi: int, as_bytes: bool = False, **kwargs):
357359
"""
@@ -400,7 +402,7 @@ def _repr_html_(self):
400402
html = '<img src="data:image/png;base64,{image}" width="{width}px">'
401403
return html.format(image=base64_png.decode("utf-8"), width=500)
402404

403-
from pygmt.src import ( # type: ignore [misc]
405+
from pygmt.src import ( # type: ignore[misc]
404406
basemap,
405407
coast,
406408
colorbar,

pygmt/tests/test_figure.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import numpy.testing as npt
1313
import pytest
1414
from pygmt import Figure, set_display
15-
from pygmt.exceptions import GMTError, GMTInvalidInput
15+
from pygmt.exceptions import GMTInvalidInput
1616
from pygmt.figure import SHOW_CONFIG, _get_default_display_method
1717
from pygmt.helpers import GMTTempFile
1818

@@ -321,7 +321,7 @@ def test_figure_show_notebook_error_without_ipython():
321321
"""
322322
fig = Figure()
323323
fig.basemap(region=[0, 1, 2, 3], frame=True)
324-
with pytest.raises(GMTError):
324+
with pytest.raises(ImportError):
325325
fig.show(method="notebook")
326326

327327

@@ -361,7 +361,7 @@ def test_set_display(self):
361361
assert mock_viewer.call_count == 0
362362
assert mock_display.call_count == 1
363363
else:
364-
with pytest.raises(GMTError):
364+
with pytest.raises(ImportError):
365365
fig.show()
366366

367367
# Test the "external" display method

0 commit comments

Comments
 (0)