Skip to content

Commit 62041f0

Browse files
committed
Use GMTTypeError exception
1 parent a9b0363 commit 62041f0

File tree

12 files changed

+56
-50
lines changed

12 files changed

+56
-50
lines changed

pygmt/clib/session.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
GMTCLibError,
2929
GMTCLibNoSessionError,
3030
GMTInvalidInput,
31+
GMTTypeError,
3132
GMTValueError,
3233
)
3334
from pygmt.helpers import (
@@ -629,7 +630,7 @@ def call_module(self, module: str, args: str | list[str]) -> None:
629630
630631
Raises
631632
------
632-
GMTInvalidInput
633+
GMTTypeError
633634
If the ``args`` argument is not a string or a list of strings.
634635
GMTCLibError
635636
If the returned status code of the function is non-zero.
@@ -658,8 +659,10 @@ def call_module(self, module: str, args: str | list[str]) -> None:
658659
mode = self["GMT_MODULE_CMD"]
659660
argv = args.encode()
660661
else:
661-
msg = "'args' must either be a list of strings (recommended) or a string."
662-
raise GMTInvalidInput(msg)
662+
raise GMTTypeError(
663+
type(args),
664+
reason="Parameter 'args' must either be a list of strings (recommended) or a string.",
665+
)
663666

664667
status = c_call_module(self.session_pointer, module.encode(), mode, argv)
665668
if status != 0:
@@ -913,9 +916,8 @@ def _check_dtype_and_dim(self, array: np.ndarray, ndim: int) -> int:
913916
914917
Raises
915918
------
916-
GMTInvalidInput
917-
If the array has the wrong number of dimensions or is an unsupported data
918-
type.
919+
GMTTypeError
920+
If the array is an unsupported data type.
919921
920922
Examples
921923
--------
@@ -939,8 +941,7 @@ def _check_dtype_and_dim(self, array: np.ndarray, ndim: int) -> int:
939941
# 1-D arrays can be numeric or text, 2-D arrays can only be numeric.
940942
valid_dtypes = DTYPES if ndim == 1 else DTYPES_NUMERIC
941943
if (dtype := array.dtype.type) not in valid_dtypes:
942-
msg = f"Unsupported numpy data type '{dtype}'."
943-
raise GMTInvalidInput(msg)
944+
raise GMTTypeError(dtype)
944945
return self[DTYPES[dtype]]
945946

946947
def put_vector(
@@ -1867,8 +1868,9 @@ def virtualfile_in( # noqa: PLR0912
18671868
elif check_kind == "vector":
18681869
valid_kinds += ("empty", "matrix", "vectors", "geojson")
18691870
if kind not in valid_kinds:
1870-
msg = f"Unrecognized data type for {check_kind}: {type(data)}."
1871-
raise GMTInvalidInput(msg)
1871+
raise GMTTypeError(
1872+
type(data), reason="Unrecognized for {check_kind!r} kind."
1873+
)
18721874

18731875
# Decide which virtualfile_from_ function to use
18741876
_virtualfile_from = {

pygmt/src/grdcut.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import xarray as xr
88
from pygmt._typing import PathLike
99
from pygmt.clib import Session
10-
from pygmt.exceptions import GMTInvalidInput, GMTValueError
10+
from pygmt.exceptions import GMTTypeError, GMTValueError
1111
from pygmt.helpers import (
1212
build_arg_list,
1313
data_kind,
@@ -122,8 +122,7 @@ def grdcut(
122122
case "file":
123123
outkind = kind
124124
case _:
125-
msg = f"Unsupported data type {type(grid)}."
126-
raise GMTInvalidInput(msg)
125+
raise GMTTypeError(type(grid))
127126

128127
with Session() as lib:
129128
with (

pygmt/src/legend.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from pygmt._typing import PathLike
88
from pygmt.clib import Session
9-
from pygmt.exceptions import GMTInvalidInput
9+
from pygmt.exceptions import GMTTypeError
1010
from pygmt.helpers import (
1111
build_arg_list,
1212
data_kind,
@@ -91,11 +91,11 @@ def legend(
9191

9292
kind = data_kind(spec)
9393
if kind not in {"empty", "file", "stringio"}:
94-
msg = f"Unrecognized data type: {type(spec)}"
95-
raise GMTInvalidInput(msg)
94+
raise GMTTypeError(type(spec))
9695
if kind == "file" and is_nonstr_iter(spec):
97-
msg = "Only one legend specification file is allowed."
98-
raise GMTInvalidInput(msg)
96+
raise GMTTypeError(
97+
type(spec), reason="Only one legend specification file is allowed."
98+
)
9999

100100
with Session() as lib:
101101
with lib.virtualfile_in(data=spec, required=False) as vintbl:

pygmt/src/plot.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from pygmt._typing import PathLike, TableLike
88
from pygmt.clib import Session
9-
from pygmt.exceptions import GMTInvalidInput
9+
from pygmt.exceptions import GMTInvalidInput, GMTTypeError
1010
from pygmt.helpers import (
1111
build_arg_list,
1212
data_kind,
@@ -272,8 +272,10 @@ def plot( # noqa: PLR0912
272272
("symbol", symbol),
273273
]:
274274
if is_nonstr_iter(value):
275-
msg = f"'{name}' can't be a 1-D array if 'data' is used."
276-
raise GMTInvalidInput(msg)
275+
raise GMTTypeError(
276+
type(value),
277+
reason=f"Parameter {name!r} can't be a 1-D array if 'data' is used.",
278+
)
277279

278280
# Set the default style if data has a geometry of Point or MultiPoint
279281
if kwargs.get("S") is None and _data_geometry_is_point(data, kind):

pygmt/src/plot3d.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from pygmt._typing import PathLike, TableLike
88
from pygmt.clib import Session
9-
from pygmt.exceptions import GMTInvalidInput
9+
from pygmt.exceptions import GMTInvalidInput, GMTTypeError
1010
from pygmt.helpers import (
1111
build_arg_list,
1212
data_kind,
@@ -251,8 +251,10 @@ def plot3d( # noqa: PLR0912
251251
("symbol", symbol),
252252
]:
253253
if is_nonstr_iter(value):
254-
msg = f"'{name}' can't be a 1-D array if 'data' is used."
255-
raise GMTInvalidInput(msg)
254+
raise GMTTypeError(
255+
type(value),
256+
reason=f"Parameter {name!r} can't be a 1-D array if 'data' is used.",
257+
)
256258

257259
# Set the default style if data has a geometry of Point or MultiPoint
258260
if kwargs.get("S") is None and _data_geometry_is_point(data, kind):

pygmt/src/text.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88
from pygmt._typing import AnchorCode, PathLike, StringArrayTypes, TableLike
99
from pygmt.clib import Session
10-
from pygmt.exceptions import GMTInvalidInput
10+
from pygmt.exceptions import GMTInvalidInput, GMTTypeError
1111
from pygmt.helpers import (
1212
_check_encoding,
1313
build_arg_list,
@@ -257,8 +257,10 @@ def text_( # noqa: PLR0912
257257

258258
for arg, _, name in [*array_args, (kwargs.get("t"), "", "transparency")]:
259259
if is_nonstr_iter(arg):
260-
msg = f"Argument of '{name}' must be a single value or True."
261-
raise GMTInvalidInput(msg)
260+
raise GMTTypeError(
261+
type(arg),
262+
reason=f"Parameter {name!r} expects a scalar value or True.",
263+
)
262264

263265
with Session() as lib:
264266
with lib.virtualfile_in(

pygmt/src/x2sys_cross.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pandas as pd
1111
from pygmt._typing import PathLike
1212
from pygmt.clib import Session
13-
from pygmt.exceptions import GMTInvalidInput
13+
from pygmt.exceptions import GMTTypeError
1414
from pygmt.helpers import (
1515
build_arg_list,
1616
data_kind,
@@ -213,8 +213,7 @@ def x2sys_cross(
213213
# Save pandas.DataFrame track data to temporary file
214214
file_contexts.append(tempfile_from_dftrack(track=track, suffix=suffix))
215215
case _:
216-
msg = f"Unrecognized data type: {type(track)}."
217-
raise GMTInvalidInput(msg)
216+
raise GMTTypeError(type(track))
218217

219218
with Session() as lib:
220219
with lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl:

pygmt/tests/test_grdcut.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77
import xarray as xr
88
from pygmt import grdcut
9-
from pygmt.exceptions import GMTInvalidInput, GMTValueError
9+
from pygmt.exceptions import GMTTypeError, GMTValueError
1010
from pygmt.helpers import GMTTempFile
1111
from pygmt.helpers.testing import load_static_earth_relief
1212

@@ -68,7 +68,7 @@ def test_grdcut_fails():
6868
"""
6969
Check that grdcut fails correctly.
7070
"""
71-
with pytest.raises(GMTInvalidInput):
71+
with pytest.raises(GMTTypeError):
7272
grdcut(np.arange(10).reshape((5, 2)))
7373

7474

pygmt/tests/test_legend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytest
99
from pygmt import Figure
10-
from pygmt.exceptions import GMTInvalidInput
10+
from pygmt.exceptions import GMTTypeError
1111
from pygmt.helpers import GMTTempFile
1212

1313

@@ -121,8 +121,8 @@ def test_legend_fails():
121121
Test legend fails with invalid spec.
122122
"""
123123
fig = Figure()
124-
with pytest.raises(GMTInvalidInput):
124+
with pytest.raises(GMTTypeError):
125125
fig.legend(spec=["@Table_5_11.txt"])
126126

127-
with pytest.raises(GMTInvalidInput):
127+
with pytest.raises(GMTTypeError):
128128
fig.legend(spec=[1, 2])

pygmt/tests/test_plot.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pytest
1111
import xarray as xr
1212
from pygmt import Figure, which
13-
from pygmt.exceptions import GMTInvalidInput
13+
from pygmt.exceptions import GMTInvalidInput, GMTTypeError
1414
from pygmt.helpers import GMTTempFile
1515

1616
POINTS_DATA = Path(__file__).parent / "data" / "points.txt"
@@ -98,15 +98,15 @@ def test_plot_fail_1d_array_with_data(data, region):
9898
"""
9999
fig = Figure()
100100
kwargs = {"data": data, "region": region, "projection": "X10c", "frame": "afg"}
101-
with pytest.raises(GMTInvalidInput):
101+
with pytest.raises(GMTTypeError):
102102
fig.plot(style="c0.2c", fill=data[:, 2], **kwargs)
103-
with pytest.raises(GMTInvalidInput):
103+
with pytest.raises(GMTTypeError):
104104
fig.plot(style="cc", size=data[:, 2], fill="red", **kwargs)
105-
with pytest.raises(GMTInvalidInput):
105+
with pytest.raises(GMTTypeError):
106106
fig.plot(style="c0.2c", fill="red", intensity=data[:, 2], **kwargs)
107-
with pytest.raises(GMTInvalidInput):
107+
with pytest.raises(GMTTypeError):
108108
fig.plot(style="c0.2c", fill="red", transparency=data[:, 2] * 100, **kwargs)
109-
with pytest.raises(GMTInvalidInput):
109+
with pytest.raises(GMTTypeError):
110110
fig.plot(style="0.2c", fill="red", symbol=["c"] * data.shape[0], **kwargs)
111111

112112

0 commit comments

Comments
 (0)