Skip to content

Commit f4f3012

Browse files
committed
Merge branch 'main' into AliasSystem/alias
2 parents fae3ab0 + eddc912 commit f4f3012

31 files changed

+113
-87
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ All custom exceptions are derived from :class:`pygmt.exceptions.GMTError`.
283283
exceptions.GMTCLibError
284284
exceptions.GMTCLibNoSessionError
285285
exceptions.GMTCLibNotFoundError
286+
exceptions.GMTTypeError
286287
exceptions.GMTValueError
287288

288289

pygmt/clib/session.py

Lines changed: 12 additions & 8 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:
@@ -915,7 +918,7 @@ def _check_dtype_and_dim(self, array: np.ndarray, ndim: int) -> int:
915918
------
916919
GMTValueError
917920
If the array has the wrong number of dimensions.
918-
GMTInvalidInput
921+
GMTTypeError
919922
If the array is an unsupported data type.
920923
921924
Examples
@@ -943,8 +946,7 @@ def _check_dtype_and_dim(self, array: np.ndarray, ndim: int) -> int:
943946
# 1-D arrays can be numeric or text, 2-D arrays can only be numeric.
944947
valid_dtypes = DTYPES if ndim == 1 else DTYPES_NUMERIC
945948
if (dtype := array.dtype.type) not in valid_dtypes:
946-
msg = f"Unsupported numpy data type '{dtype}'."
947-
raise GMTInvalidInput(msg)
949+
raise GMTTypeError(dtype)
948950
return self[DTYPES[dtype]]
949951

950952
def put_vector(
@@ -1871,8 +1873,10 @@ def virtualfile_in( # noqa: PLR0912
18711873
elif check_kind == "vector":
18721874
valid_kinds += ("empty", "matrix", "vectors", "geojson")
18731875
if kind not in valid_kinds:
1874-
msg = f"Unrecognized data type for {check_kind}: {type(data)}."
1875-
raise GMTInvalidInput(msg)
1876+
raise GMTTypeError(
1877+
type(data),
1878+
reason=f"Unrecognized data type for {check_kind!r} kind.",
1879+
)
18761880

18771881
# Decide which virtualfile_from_ function to use
18781882
_virtualfile_from = {

pygmt/exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,18 @@ def __init__(
115115
if reason:
116116
msg += f" {reason}"
117117
super().__init__(msg)
118+
119+
120+
class GMTTypeError(GMTError, TypeError):
121+
"""
122+
Raised when an invalid type is passed to a function/method.
123+
124+
This exception is used to indicate that the type of an argument does not match
125+
the expected type.
126+
"""
127+
128+
def __init__(self, dtype: object, /, reason: str | None = None):
129+
msg = f"Unrecognized data type: {dtype!r}."
130+
if reason:
131+
msg += f" {reason}"
132+
super().__init__(msg)

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 single value or True.",
263+
)
262264

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

pygmt/src/velo.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pandas as pd
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
fmt_docstring,
@@ -248,11 +248,13 @@ def velo(self, data: PathLike | TableLike | None = None, **kwargs):
248248
raise GMTInvalidInput(msg)
249249

250250
if isinstance(data, np.ndarray) and not pd.api.types.is_numeric_dtype(data):
251-
msg = (
252-
"Text columns are not supported with numpy.ndarray type inputs. "
253-
"They are only supported with file or pandas.DataFrame inputs."
251+
raise GMTTypeError(
252+
type(data),
253+
reason=(
254+
"Text columns are not supported with numpy.ndarray type inputs. "
255+
"They are only supported with file or pandas.DataFrame inputs."
256+
),
254257
)
255-
raise GMTInvalidInput(msg)
256258

257259
with Session() as lib:
258260
with lib.virtualfile_in(check_kind="vector", data=data) as vintbl:

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:

0 commit comments

Comments
 (0)