From a9b036324168df509bc3e6258c746572bc2955b1 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 15 Jul 2025 11:23:40 +0800 Subject: [PATCH 1/7] Add the GMTTypeError exception --- pygmt/exceptions.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pygmt/exceptions.py b/pygmt/exceptions.py index 3d19fafdc32..dedb64db7fa 100644 --- a/pygmt/exceptions.py +++ b/pygmt/exceptions.py @@ -115,3 +115,18 @@ def __init__( if reason: msg += f" {reason}" super().__init__(msg) + + +class GMTTypeError(GMTError, TypeError): + """ + Raised when an invalid type is passed to a function/method. + + This exception is used to indicate that the type of an argument does not match + the expected type. + """ + + def __init__(self, dtype: object, /, reason: str | None = None): + msg = f"Unrecognized data type: {dtype!r}." + if reason: + msg += f" {reason}" + super().__init__(msg) From 62041f0f6675e200a4e83612c3c526e8a7597ac9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 15 Jul 2025 11:24:01 +0800 Subject: [PATCH 2/7] Use GMTTypeError exception --- pygmt/clib/session.py | 22 ++++++++++++---------- pygmt/src/grdcut.py | 5 ++--- pygmt/src/legend.py | 10 +++++----- pygmt/src/plot.py | 8 +++++--- pygmt/src/plot3d.py | 8 +++++--- pygmt/src/text.py | 8 +++++--- pygmt/src/x2sys_cross.py | 5 ++--- pygmt/tests/test_grdcut.py | 4 ++-- pygmt/tests/test_legend.py | 6 +++--- pygmt/tests/test_plot.py | 12 ++++++------ pygmt/tests/test_plot3d.py | 10 +++++----- pygmt/tests/test_x2sys_cross.py | 8 ++++---- 12 files changed, 56 insertions(+), 50 deletions(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 4d2e89f2399..b7ddbcefa2f 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -28,6 +28,7 @@ GMTCLibError, GMTCLibNoSessionError, GMTInvalidInput, + GMTTypeError, GMTValueError, ) from pygmt.helpers import ( @@ -629,7 +630,7 @@ def call_module(self, module: str, args: str | list[str]) -> None: Raises ------ - GMTInvalidInput + GMTTypeError If the ``args`` argument is not a string or a list of strings. GMTCLibError 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: mode = self["GMT_MODULE_CMD"] argv = args.encode() else: - msg = "'args' must either be a list of strings (recommended) or a string." - raise GMTInvalidInput(msg) + raise GMTTypeError( + type(args), + reason="Parameter 'args' must either be a list of strings (recommended) or a string.", + ) status = c_call_module(self.session_pointer, module.encode(), mode, argv) if status != 0: @@ -913,9 +916,8 @@ def _check_dtype_and_dim(self, array: np.ndarray, ndim: int) -> int: Raises ------ - GMTInvalidInput - If the array has the wrong number of dimensions or is an unsupported data - type. + GMTTypeError + If the array is an unsupported data type. Examples -------- @@ -939,8 +941,7 @@ def _check_dtype_and_dim(self, array: np.ndarray, ndim: int) -> int: # 1-D arrays can be numeric or text, 2-D arrays can only be numeric. valid_dtypes = DTYPES if ndim == 1 else DTYPES_NUMERIC if (dtype := array.dtype.type) not in valid_dtypes: - msg = f"Unsupported numpy data type '{dtype}'." - raise GMTInvalidInput(msg) + raise GMTTypeError(dtype) return self[DTYPES[dtype]] def put_vector( @@ -1867,8 +1868,9 @@ def virtualfile_in( # noqa: PLR0912 elif check_kind == "vector": valid_kinds += ("empty", "matrix", "vectors", "geojson") if kind not in valid_kinds: - msg = f"Unrecognized data type for {check_kind}: {type(data)}." - raise GMTInvalidInput(msg) + raise GMTTypeError( + type(data), reason="Unrecognized for {check_kind!r} kind." + ) # Decide which virtualfile_from_ function to use _virtualfile_from = { diff --git a/pygmt/src/grdcut.py b/pygmt/src/grdcut.py index 8f9215d61f8..5ebf9f736db 100644 --- a/pygmt/src/grdcut.py +++ b/pygmt/src/grdcut.py @@ -7,7 +7,7 @@ import xarray as xr from pygmt._typing import PathLike from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput, GMTValueError +from pygmt.exceptions import GMTTypeError, GMTValueError from pygmt.helpers import ( build_arg_list, data_kind, @@ -122,8 +122,7 @@ def grdcut( case "file": outkind = kind case _: - msg = f"Unsupported data type {type(grid)}." - raise GMTInvalidInput(msg) + raise GMTTypeError(type(grid)) with Session() as lib: with ( diff --git a/pygmt/src/legend.py b/pygmt/src/legend.py index 2cb2eddcf95..07c565ea012 100644 --- a/pygmt/src/legend.py +++ b/pygmt/src/legend.py @@ -6,7 +6,7 @@ from pygmt._typing import PathLike from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers import ( build_arg_list, data_kind, @@ -91,11 +91,11 @@ def legend( kind = data_kind(spec) if kind not in {"empty", "file", "stringio"}: - msg = f"Unrecognized data type: {type(spec)}" - raise GMTInvalidInput(msg) + raise GMTTypeError(type(spec)) if kind == "file" and is_nonstr_iter(spec): - msg = "Only one legend specification file is allowed." - raise GMTInvalidInput(msg) + raise GMTTypeError( + type(spec), reason="Only one legend specification file is allowed." + ) with Session() as lib: with lib.virtualfile_in(data=spec, required=False) as vintbl: diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 35dab4aa009..496f241546f 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -6,7 +6,7 @@ from pygmt._typing import PathLike, TableLike from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTTypeError from pygmt.helpers import ( build_arg_list, data_kind, @@ -272,8 +272,10 @@ def plot( # noqa: PLR0912 ("symbol", symbol), ]: if is_nonstr_iter(value): - msg = f"'{name}' can't be a 1-D array if 'data' is used." - raise GMTInvalidInput(msg) + raise GMTTypeError( + type(value), + reason=f"Parameter {name!r} can't be a 1-D array if 'data' is used.", + ) # Set the default style if data has a geometry of Point or MultiPoint if kwargs.get("S") is None and _data_geometry_is_point(data, kind): diff --git a/pygmt/src/plot3d.py b/pygmt/src/plot3d.py index 491ebbcf9df..53afddd14c8 100644 --- a/pygmt/src/plot3d.py +++ b/pygmt/src/plot3d.py @@ -6,7 +6,7 @@ from pygmt._typing import PathLike, TableLike from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTTypeError from pygmt.helpers import ( build_arg_list, data_kind, @@ -251,8 +251,10 @@ def plot3d( # noqa: PLR0912 ("symbol", symbol), ]: if is_nonstr_iter(value): - msg = f"'{name}' can't be a 1-D array if 'data' is used." - raise GMTInvalidInput(msg) + raise GMTTypeError( + type(value), + reason=f"Parameter {name!r} can't be a 1-D array if 'data' is used.", + ) # Set the default style if data has a geometry of Point or MultiPoint if kwargs.get("S") is None and _data_geometry_is_point(data, kind): diff --git a/pygmt/src/text.py b/pygmt/src/text.py index 99a3180415a..73ec02f8c78 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -7,7 +7,7 @@ import numpy as np from pygmt._typing import AnchorCode, PathLike, StringArrayTypes, TableLike from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTTypeError from pygmt.helpers import ( _check_encoding, build_arg_list, @@ -257,8 +257,10 @@ def text_( # noqa: PLR0912 for arg, _, name in [*array_args, (kwargs.get("t"), "", "transparency")]: if is_nonstr_iter(arg): - msg = f"Argument of '{name}' must be a single value or True." - raise GMTInvalidInput(msg) + raise GMTTypeError( + type(arg), + reason=f"Parameter {name!r} expects a scalar value or True.", + ) with Session() as lib: with lib.virtualfile_in( diff --git a/pygmt/src/x2sys_cross.py b/pygmt/src/x2sys_cross.py index d502d72c190..251f1382efb 100644 --- a/pygmt/src/x2sys_cross.py +++ b/pygmt/src/x2sys_cross.py @@ -10,7 +10,7 @@ import pandas as pd from pygmt._typing import PathLike from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers import ( build_arg_list, data_kind, @@ -213,8 +213,7 @@ def x2sys_cross( # Save pandas.DataFrame track data to temporary file file_contexts.append(tempfile_from_dftrack(track=track, suffix=suffix)) case _: - msg = f"Unrecognized data type: {type(track)}." - raise GMTInvalidInput(msg) + raise GMTTypeError(type(track)) with Session() as lib: with lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl: diff --git a/pygmt/tests/test_grdcut.py b/pygmt/tests/test_grdcut.py index 8acb1736ec2..1cd54caf4b6 100644 --- a/pygmt/tests/test_grdcut.py +++ b/pygmt/tests/test_grdcut.py @@ -6,7 +6,7 @@ import pytest import xarray as xr from pygmt import grdcut -from pygmt.exceptions import GMTInvalidInput, GMTValueError +from pygmt.exceptions import GMTTypeError, GMTValueError from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -68,7 +68,7 @@ def test_grdcut_fails(): """ Check that grdcut fails correctly. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): grdcut(np.arange(10).reshape((5, 2))) diff --git a/pygmt/tests/test_legend.py b/pygmt/tests/test_legend.py index 3a63d74166e..2fba712a4dd 100644 --- a/pygmt/tests/test_legend.py +++ b/pygmt/tests/test_legend.py @@ -7,7 +7,7 @@ import pytest from pygmt import Figure -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers import GMTTempFile @@ -121,8 +121,8 @@ def test_legend_fails(): Test legend fails with invalid spec. """ fig = Figure() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.legend(spec=["@Table_5_11.txt"]) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.legend(spec=[1, 2]) diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index c2f2b846724..c38f195b958 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -10,7 +10,7 @@ import pytest import xarray as xr from pygmt import Figure, which -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTTypeError from pygmt.helpers import GMTTempFile POINTS_DATA = Path(__file__).parent / "data" / "points.txt" @@ -98,15 +98,15 @@ def test_plot_fail_1d_array_with_data(data, region): """ fig = Figure() kwargs = {"data": data, "region": region, "projection": "X10c", "frame": "afg"} - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.plot(style="c0.2c", fill=data[:, 2], **kwargs) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.plot(style="cc", size=data[:, 2], fill="red", **kwargs) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.plot(style="c0.2c", fill="red", intensity=data[:, 2], **kwargs) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.plot(style="c0.2c", fill="red", transparency=data[:, 2] * 100, **kwargs) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.plot(style="0.2c", fill="red", symbol=["c"] * data.shape[0], **kwargs) diff --git a/pygmt/tests/test_plot3d.py b/pygmt/tests/test_plot3d.py index a1f4e306d41..69237e02447 100644 --- a/pygmt/tests/test_plot3d.py +++ b/pygmt/tests/test_plot3d.py @@ -7,7 +7,7 @@ import numpy as np import pytest from pygmt import Figure -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTTypeError from pygmt.helpers import GMTTempFile POINTS_DATA = Path(__file__).parent / "data" / "points.txt" @@ -78,13 +78,13 @@ def test_plot3d_fail_1d_array_with_data(data, region): """ fig = Figure() kwargs = {"data": data, "region": region, "projection": "X10c", "frame": "afg"} - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.plot3d(style="c0.2c", fill=data[:, 2], **kwargs) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.plot3d(style="cc", size=data[:, 2], fill="red", **kwargs) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.plot3d(style="cc", intensity=data[:, 2], fill="red", **kwargs) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.plot3d(style="cc", fill="red", transparency=data[:, 2] * 100, **kwargs) diff --git a/pygmt/tests/test_x2sys_cross.py b/pygmt/tests/test_x2sys_cross.py index 50b9d5f3f86..8e0a21ab3e6 100644 --- a/pygmt/tests/test_x2sys_cross.py +++ b/pygmt/tests/test_x2sys_cross.py @@ -16,7 +16,7 @@ from pygmt import config, x2sys_cross, x2sys_init from pygmt.clib import __gmt_version__ from pygmt.datasets import load_sample_data -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError @pytest.fixture(name="mock_x2sys_home") @@ -239,11 +239,11 @@ def test_x2sys_cross_input_two_filenames(): def test_x2sys_cross_invalid_tracks_input_type(tracks): """ - Run x2sys_cross using tracks input that is not a pandas.DataFrame or str type, - which would raise a GMTInvalidInput error. + Run x2sys_cross using tracks input that is not a pandas.DataFrame or str type, which + would raise GMTTypeError. """ invalid_tracks = tracks[0].to_xarray().z - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): x2sys_cross(tracks=[invalid_tracks]) From d68a0cb33b5a56b9dbce2fa13940bf5a74f68481 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 15 Jul 2025 11:39:40 +0800 Subject: [PATCH 3/7] Fix some tests --- pygmt/clib/session.py | 2 +- pygmt/tests/test_blockm.py | 4 ++-- pygmt/tests/test_blockmedian.py | 4 ++-- pygmt/tests/test_clib_call_module.py | 4 ++-- pygmt/tests/test_grd2cpt.py | 4 ++-- pygmt/tests/test_grdfilter.py | 4 ++-- pygmt/tests/test_grdimage.py | 4 ++-- pygmt/tests/test_grdinfo.py | 4 ++-- pygmt/tests/test_grdtrack.py | 6 +++--- pygmt/tests/test_grdview.py | 6 +++--- pygmt/tests/test_surface.py | 4 ++-- 11 files changed, 23 insertions(+), 23 deletions(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index b7ddbcefa2f..b7b253c85ae 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1869,7 +1869,7 @@ def virtualfile_in( # noqa: PLR0912 valid_kinds += ("empty", "matrix", "vectors", "geojson") if kind not in valid_kinds: raise GMTTypeError( - type(data), reason="Unrecognized for {check_kind!r} kind." + type(data), reason=f"Unrecognized for {check_kind!r} kind." ) # Decide which virtualfile_from_ function to use diff --git a/pygmt/tests/test_blockm.py b/pygmt/tests/test_blockm.py index a0e2ee49d4e..b6d01d4e1f1 100644 --- a/pygmt/tests/test_blockm.py +++ b/pygmt/tests/test_blockm.py @@ -11,7 +11,7 @@ import xarray as xr from pygmt import blockmean, blockmode from pygmt.datasets import load_sample_data -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers import GMTTempFile @@ -68,7 +68,7 @@ def test_blockmean_wrong_kind_of_input_table_grid(dataframe): Run blockmean using table input that is not a pandas.DataFrame or file but a grid. """ invalid_table = dataframe.bathymetry.to_xarray() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): blockmean(data=invalid_table, spacing="5m", region=[245, 255, 20, 30]) diff --git a/pygmt/tests/test_blockmedian.py b/pygmt/tests/test_blockmedian.py index 64d642f424d..9bd4e08301d 100644 --- a/pygmt/tests/test_blockmedian.py +++ b/pygmt/tests/test_blockmedian.py @@ -9,7 +9,7 @@ import pytest from pygmt import blockmedian from pygmt.datasets import load_sample_data -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers import GMTTempFile @@ -65,7 +65,7 @@ def test_blockmedian_wrong_kind_of_input_table_grid(dataframe): Run blockmedian using table input that is not a pandas.DataFrame or file but a grid. """ invalid_table = dataframe.bathymetry.to_xarray() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): blockmedian(data=invalid_table, spacing="5m", region=[245, 255, 20, 30]) diff --git a/pygmt/tests/test_clib_call_module.py b/pygmt/tests/test_clib_call_module.py index de881b7a1d8..b4d5460f089 100644 --- a/pygmt/tests/test_clib_call_module.py +++ b/pygmt/tests/test_clib_call_module.py @@ -6,7 +6,7 @@ import pytest from pygmt import Figure, clib -from pygmt.exceptions import GMTCLibError, GMTInvalidInput +from pygmt.exceptions import GMTCLibError, GMTTypeError from pygmt.helpers import GMTTempFile POINTS_DATA = Path(__file__).parent / "data" / "points.txt" @@ -53,7 +53,7 @@ def test_call_module_invalid_argument_type(): call_module only accepts a string or a list of strings as module arguments. """ with clib.Session() as lib: - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): lib.call_module("get", ("FONT_TITLE", "FONT_TAG")) diff --git a/pygmt/tests/test_grd2cpt.py b/pygmt/tests/test_grd2cpt.py index c2d9a257cc1..e97468dc8b6 100644 --- a/pygmt/tests/test_grd2cpt.py +++ b/pygmt/tests/test_grd2cpt.py @@ -6,7 +6,7 @@ import pytest from pygmt import Figure, grd2cpt -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTTypeError from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -62,7 +62,7 @@ def test_grd2cpt_unrecognized_data_type(): """ Test that an error will be raised if an invalid data type is passed to grid. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): grd2cpt(grid=0) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index 463a3d5c3ea..ab2863a29e8 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -9,7 +9,7 @@ import xarray as xr from pygmt import grdfilter from pygmt.enums import GridRegistration, GridType -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -79,5 +79,5 @@ def test_grdfilter_fails(): """ Check that grdfilter fails correctly. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): grdfilter(np.arange(10).reshape((5, 2))) diff --git a/pygmt/tests/test_grdimage.py b/pygmt/tests/test_grdimage.py index f92dbeb3587..8df8aab6df9 100644 --- a/pygmt/tests/test_grdimage.py +++ b/pygmt/tests/test_grdimage.py @@ -10,7 +10,7 @@ from pygmt.clib import __gmt_version__ from pygmt.datasets import load_earth_relief from pygmt.enums import GridRegistration, GridType -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTTypeError from pygmt.helpers.testing import check_figures_equal @@ -156,7 +156,7 @@ def test_grdimage_fails(): Should fail for unrecognized input. """ fig = Figure() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.grdimage(np.arange(20).reshape((4, 5))) diff --git a/pygmt/tests/test_grdinfo.py b/pygmt/tests/test_grdinfo.py index f993e4b73db..cb37568f57e 100644 --- a/pygmt/tests/test_grdinfo.py +++ b/pygmt/tests/test_grdinfo.py @@ -5,7 +5,7 @@ import numpy as np import pytest from pygmt import grdinfo -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers.testing import load_static_earth_relief @@ -30,7 +30,7 @@ def test_grdinfo_fails(): """ Check that grdinfo fails correctly. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): grdinfo(np.arange(10).reshape((5, 2))) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index 8bfb6cf98fb..1b63dae041a 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -9,7 +9,7 @@ import pandas as pd import pytest from pygmt import grdtrack -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTTypeError from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -129,7 +129,7 @@ def test_grdtrack_wrong_kind_of_points_input(dataarray, dataframe): Run grdtrack using points input that is not a pandas.DataFrame or file. """ invalid_points = dataframe.longitude.to_xarray() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): grdtrack(points=invalid_points, grid=dataarray, newcolname="bathymetry") @@ -138,7 +138,7 @@ def test_grdtrack_wrong_kind_of_grid_input(dataarray, dataframe): Run grdtrack using grid input that is not an xarray.DataArray or file. """ invalid_grid = dataarray.to_dataset() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): grdtrack(points=dataframe, grid=invalid_grid, newcolname="bathymetry") diff --git a/pygmt/tests/test_grdview.py b/pygmt/tests/test_grdview.py index 3be4ed7aa42..40c40190d21 100644 --- a/pygmt/tests/test_grdview.py +++ b/pygmt/tests/test_grdview.py @@ -4,7 +4,7 @@ import pytest from pygmt import Figure, grdcut -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -59,7 +59,7 @@ def test_grdview_wrong_kind_of_grid(xrgrid): """ dataset = xrgrid.to_dataset() # convert xarray.DataArray to xarray.Dataset fig = Figure() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.grdview(grid=dataset) @@ -237,5 +237,5 @@ def test_grdview_wrong_kind_of_drapegrid(xrgrid): """ dataset = xrgrid.to_dataset() # convert xarray.DataArray to xarray.Dataset fig = Figure() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.grdview(grid=xrgrid, drapegrid=dataset) diff --git a/pygmt/tests/test_surface.py b/pygmt/tests/test_surface.py index ab075a045ab..b065137aa87 100644 --- a/pygmt/tests/test_surface.py +++ b/pygmt/tests/test_surface.py @@ -9,7 +9,7 @@ import xarray as xr from pygmt import surface, which from pygmt.enums import GridRegistration, GridType -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers import GMTTempFile @@ -126,7 +126,7 @@ def test_surface_wrong_kind_of_input(data, region, spacing): Run surface using grid input that is not file/matrix/vectors. """ data = data.z.to_xarray() # convert pandas.Series to xarray.DataArray - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): surface(data=data, spacing=spacing, region=region) From 5cdb38ed30b2373952f74be14a76081d1741d43d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 15 Jul 2025 13:50:34 +0800 Subject: [PATCH 4/7] Fix more tests --- pygmt/tests/test_clib_put_vector.py | 4 ++-- pygmt/tests/test_grdcontour.py | 4 ++-- pygmt/tests/test_info.py | 4 ++-- pygmt/tests/test_nearneighbor.py | 4 ++-- pygmt/tests/test_triangulate.py | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pygmt/tests/test_clib_put_vector.py b/pygmt/tests/test_clib_put_vector.py index cad77ba59cf..913ab38d507 100644 --- a/pygmt/tests/test_clib_put_vector.py +++ b/pygmt/tests/test_clib_put_vector.py @@ -10,7 +10,7 @@ import pytest from pygmt import clib from pygmt.clib.session import DTYPES_NUMERIC -from pygmt.exceptions import GMTCLibError, GMTInvalidInput +from pygmt.exceptions import GMTCLibError, GMTInvalidInput, GMTTypeError from pygmt.helpers import GMTTempFile @@ -225,7 +225,7 @@ def test_put_vector_invalid_dtype(): dim=[2, 3, 0, 0], # ncolumns, nrows, dtype, unused ) data = np.array([37, 12, 556], dtype=dtype) - with pytest.raises(GMTInvalidInput, match="Unsupported numpy data type"): + with pytest.raises(GMTTypeError, match="Invalida data type"): lib.put_vector(dataset, column=0, vector=data) diff --git a/pygmt/tests/test_grdcontour.py b/pygmt/tests/test_grdcontour.py index b2e1558db86..4c19f69df0b 100644 --- a/pygmt/tests/test_grdcontour.py +++ b/pygmt/tests/test_grdcontour.py @@ -7,7 +7,7 @@ import numpy as np import pytest from pygmt import Figure -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers.testing import load_static_earth_relief TEST_CONTOUR_FILE = Path(__file__).parent / "data" / "contours.txt" @@ -123,5 +123,5 @@ def test_grdcontour_fails(): Should fail for unrecognized input. """ fig = Figure() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): fig.grdcontour(np.arange(20).reshape((4, 5))) diff --git a/pygmt/tests/test_info.py b/pygmt/tests/test_info.py index d055abb61ec..8243f613dbc 100644 --- a/pygmt/tests/test_info.py +++ b/pygmt/tests/test_info.py @@ -11,7 +11,7 @@ import pytest import xarray as xr from pygmt import info -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers.testing import skip_if_no POINTS_DATA = Path(__file__).parent / "data" / "points.txt" @@ -245,7 +245,7 @@ def test_info_fails(): Make sure info raises an exception if not given either a file name, pandas DataFrame, or numpy ndarray. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): info(data=xr.DataArray(21)) diff --git a/pygmt/tests/test_nearneighbor.py b/pygmt/tests/test_nearneighbor.py index 49522a4a171..eef6c29944f 100644 --- a/pygmt/tests/test_nearneighbor.py +++ b/pygmt/tests/test_nearneighbor.py @@ -11,7 +11,7 @@ from pygmt import nearneighbor from pygmt.datasets import load_sample_data from pygmt.enums import GridRegistration, GridType -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTTypeError from pygmt.helpers import GMTTempFile @@ -62,7 +62,7 @@ def test_nearneighbor_wrong_kind_of_input(ship_data): Run nearneighbor using grid input that is not file/matrix/vectors. """ data = ship_data.bathymetry.to_xarray() # convert pandas.Series to xarray.DataArray - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): nearneighbor( data=data, spacing="5m", region=[245, 255, 20, 30], search_radius="10m" ) diff --git a/pygmt/tests/test_triangulate.py b/pygmt/tests/test_triangulate.py index 4e4ba794240..353f058284e 100644 --- a/pygmt/tests/test_triangulate.py +++ b/pygmt/tests/test_triangulate.py @@ -10,7 +10,7 @@ import xarray as xr from pygmt import triangulate, which from pygmt.enums import GridRegistration, GridType -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTTypeError from pygmt.helpers import GMTTempFile @@ -94,7 +94,7 @@ def test_delaunay_triples_wrong_kind_of_input(dataframe): Run triangulate.delaunay_triples using grid input that is not file/matrix/vectors. """ data = dataframe.z.to_xarray() # convert pandas.Series to xarray.DataArray - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTTypeError): triangulate.delaunay_triples(data=data) From 74c632fb04fdc450e1e35105d3b73f2d0fba8db9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 15 Jul 2025 13:52:49 +0800 Subject: [PATCH 5/7] Fix a typo --- pygmt/tests/test_clib_put_vector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_clib_put_vector.py b/pygmt/tests/test_clib_put_vector.py index 913ab38d507..56b5beaade0 100644 --- a/pygmt/tests/test_clib_put_vector.py +++ b/pygmt/tests/test_clib_put_vector.py @@ -225,7 +225,7 @@ def test_put_vector_invalid_dtype(): dim=[2, 3, 0, 0], # ncolumns, nrows, dtype, unused ) data = np.array([37, 12, 556], dtype=dtype) - with pytest.raises(GMTTypeError, match="Invalida data type"): + with pytest.raises(GMTTypeError, match="Invalid data type"): lib.put_vector(dataset, column=0, vector=data) From f5e44c438c8d29700e64d0d1e5947478949605ab Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 15 Jul 2025 14:00:10 +0800 Subject: [PATCH 6/7] Fix a typo --- pygmt/tests/test_clib_put_vector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_clib_put_vector.py b/pygmt/tests/test_clib_put_vector.py index 56b5beaade0..54c4694cb6b 100644 --- a/pygmt/tests/test_clib_put_vector.py +++ b/pygmt/tests/test_clib_put_vector.py @@ -225,7 +225,7 @@ def test_put_vector_invalid_dtype(): dim=[2, 3, 0, 0], # ncolumns, nrows, dtype, unused ) data = np.array([37, 12, 556], dtype=dtype) - with pytest.raises(GMTTypeError, match="Invalid data type"): + with pytest.raises(GMTTypeError, match="Unrecognized data type"): lib.put_vector(dataset, column=0, vector=data) From bc3edefb686a84e0fe3155ca23ca1efafc4b4b1c Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 15 Jul 2025 16:18:43 +0800 Subject: [PATCH 7/7] Add to API doc --- doc/api/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/index.rst b/doc/api/index.rst index 5c1b2072d6b..1c84a94dc84 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -284,6 +284,7 @@ All custom exceptions are derived from :class:`pygmt.exceptions.GMTError`. exceptions.GMTCLibNoSessionError exceptions.GMTCLibNotFoundError exceptions.GMTValueError + exceptions.GMTTypeError .. currentmodule:: pygmt