Skip to content

Commit 170d11b

Browse files
committed
More fixes
1 parent b14dccf commit 170d11b

11 files changed

+39
-32
lines changed

pygmt/datasets/load_remote_dataset.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Any, Literal, NamedTuple
88

99
import xarray as xr
10-
from pygmt.exceptions import GMTInvalidInput, GMTValueError
10+
from pygmt.exceptions import GMTParameterError, GMTValueError
1111

1212
with contextlib.suppress(ImportError):
1313
# rioxarray is needed to register the rio accessor
@@ -572,11 +572,13 @@ def _load_remote_dataset(
572572
reg = registration[0]
573573

574574
if resinfo.tiled and region is None:
575-
msg = (
576-
f"The 'region' parameter is required for {dataset.description} "
577-
f"resolution '{resolution}'."
575+
raise GMTParameterError(
576+
required=["region"],
577+
reason=(
578+
f"Parameter 'region' is required for {dataset.description} resolution "
579+
f"{resolution!r} with tiled grids."
580+
),
578581
)
579-
raise GMTInvalidInput(msg)
580582

581583
fname = f"@{prefix}_{resolution}_{reg}"
582584
grid = xr.load_dataarray(

pygmt/src/grdfill.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import xarray as xr
99
from pygmt._typing import PathLike
1010
from pygmt.clib import Session
11-
from pygmt.exceptions import GMTInvalidInput, GMTParameterError
11+
from pygmt.exceptions import GMTParameterError
1212
from pygmt.helpers import (
1313
build_arg_list,
1414
deprecate_parameter,
@@ -45,7 +45,7 @@ def _validate_params(
4545
>>> _validate_params()
4646
Traceback (most recent call last):
4747
...
48-
pygmt.exceptions.GMTInvalidInput: Need to specify parameter ...
48+
pygmt.exceptions.GMTParameterError: ...
4949
"""
5050
_fill_params = {"constantfill", "gridfill", "neighborfill", "splinefill"}
5151
# The deprecated 'mode' parameter is given.
@@ -63,11 +63,13 @@ def _validate_params(
6363
if n_given > 1: # More than one mutually exclusive parameter is given.
6464
raise GMTParameterError(exclusive=[*_fill_params, "inquire", "mode"])
6565
if n_given == 0: # No parameters are given.
66-
msg = (
67-
f"Need to specify parameter {_fill_params} for filling holes or "
68-
"'inquire' for inquiring the bounds of each hole."
66+
raise GMTParameterError(
67+
required=_fill_params,
68+
reason=(
69+
f"Need to specify parameter {_fill_params!r} for filling holes or "
70+
"'inquire' for inquiring the bounds of each hole."
71+
),
6972
)
70-
raise GMTInvalidInput(msg)
7173

7274

7375
def _parse_fill_mode(

pygmt/src/grdgradient.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import xarray as xr
66
from pygmt._typing import PathLike
77
from pygmt.clib import Session
8-
from pygmt.exceptions import GMTInvalidInput, GMTParameterError
8+
from pygmt.exceptions import GMTParameterError
99
from pygmt.helpers import (
1010
args_in_kwargs,
1111
build_arg_list,
@@ -162,8 +162,10 @@ def grdgradient(
162162
>>> new_grid = pygmt.grdgradient(grid=grid, azimuth=10)
163163
"""
164164
if kwargs.get("Q") is not None and kwargs.get("N") is None:
165-
msg = "Must specify normalize if tiles is specified."
166-
raise GMTInvalidInput(msg)
165+
raise GMTParameterError(
166+
required=["normalize"],
167+
reason="Must specify 'normalize' if 'tiles' is specified.",
168+
)
167169
if not args_in_kwargs(args=["A", "D", "E"], kwargs=kwargs):
168170
raise GMTParameterError(require_any={"azimuth", "direction", "radiance"})
169171

pygmt/src/grdimage.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import xarray as xr
66
from pygmt._typing import PathLike
77
from pygmt.clib import Session
8-
from pygmt.exceptions import GMTInvalidInput
98
from pygmt.helpers import (
109
build_arg_list,
1110
fmt_docstring,
@@ -164,7 +163,7 @@ def grdimage(self, grid: PathLike | xr.DataArray, **kwargs):
164163
"Parameter 'img_out'/'A' is not implemented. "
165164
"Please consider submitting a feature request to us."
166165
)
167-
raise GMTInvalidInput(msg)
166+
raise NotImplementedError(msg)
168167

169168
with Session() as lib:
170169
with (

pygmt/src/project.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pandas as pd
99
from pygmt._typing import PathLike, TableLike
1010
from pygmt.clib import Session
11-
from pygmt.exceptions import GMTInvalidInput, GMTParameterError
11+
from pygmt.exceptions import GMTParameterError
1212
from pygmt.helpers import (
1313
build_arg_list,
1414
fmt_docstring,
@@ -225,8 +225,10 @@ def project(
225225
if kwargs.get("C") is None:
226226
raise GMTParameterError(required={"center"})
227227
if kwargs.get("G") is None and data is None:
228-
msg = "The 'data' parameter must be specified unless 'generate' is used."
229-
raise GMTInvalidInput(msg)
228+
raise GMTParameterError(
229+
required={"data"},
230+
reason="Parameter 'data' must be specified unless 'generate' is used.",
231+
)
230232
if kwargs.get("G") is not None and kwargs.get("F") is not None:
231233
raise GMTParameterError(exclusive={"generate", "convention"})
232234

pygmt/tests/test_datasets_load_remote_datasets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66
from pygmt.datasets.load_remote_dataset import _load_remote_dataset
77
from pygmt.enums import GridRegistration
8-
from pygmt.exceptions import GMTInvalidInput, GMTValueError
8+
from pygmt.exceptions import GMTParameterError, GMTValueError
99

1010

1111
def load_remote_dataset_wrapper(resolution="01d", region=None, registration=None):
@@ -61,7 +61,7 @@ def test_load_remote_dataset_tiled_grid_without_region():
6161
Make sure _load_remote_dataset fails when trying to load a tiled grid without
6262
specifying a region.
6363
"""
64-
with pytest.raises(GMTInvalidInput):
64+
with pytest.raises(GMTParameterError):
6565
load_remote_dataset_wrapper(resolution="01m")
6666

6767

pygmt/tests/test_grdfill.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import xarray as xr
1111
from pygmt import grdfill
1212
from pygmt.enums import GridRegistration, GridType
13-
from pygmt.exceptions import GMTInvalidInput, GMTParameterError
13+
from pygmt.exceptions import GMTParameterError
1414
from pygmt.helpers import GMTTempFile
1515
from pygmt.helpers.testing import load_static_earth_relief
1616

@@ -128,7 +128,7 @@ def test_grdfill_required_args(grid):
128128
"""
129129
Test that grdfill fails without filling parameters or 'inquire'.
130130
"""
131-
with pytest.raises(GMTInvalidInput):
131+
with pytest.raises(GMTParameterError):
132132
grdfill(grid=grid)
133133

134134

pygmt/tests/test_grdgradient.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import xarray as xr
99
from pygmt import grdgradient
1010
from pygmt.enums import GridRegistration, GridType
11-
from pygmt.exceptions import GMTInvalidInput, GMTParameterError
11+
from pygmt.exceptions import GMTParameterError
1212
from pygmt.helpers import GMTTempFile
1313
from pygmt.helpers.testing import load_static_earth_relief
1414

@@ -82,6 +82,6 @@ def test_grdgradient_fails(grid):
8282
"""
8383
with pytest.raises(GMTParameterError):
8484
grdgradient(grid=grid) # fails without required arguments
85-
with pytest.raises(GMTInvalidInput):
85+
with pytest.raises(GMTParameterError):
8686
# fails when tiles is specified but not normalize
8787
grdgradient(grid=grid, azimuth=10, direction="c", tiles="c")

pygmt/tests/test_grdimage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pygmt.clib import __gmt_version__
1111
from pygmt.datasets import load_earth_relief
1212
from pygmt.enums import GridRegistration, GridType
13-
from pygmt.exceptions import GMTInvalidInput, GMTTypeError
13+
from pygmt.exceptions import GMTTypeError
1414
from pygmt.helpers.testing import check_figures_equal
1515

1616

@@ -252,9 +252,9 @@ def test_grdimage_imgout_fails(grid):
252252
Test that an exception is raised if img_out/A is given.
253253
"""
254254
fig = Figure()
255-
with pytest.raises(GMTInvalidInput):
255+
with pytest.raises(NotImplementedError):
256256
fig.grdimage(grid, img_out="out.png")
257-
with pytest.raises(GMTInvalidInput):
257+
with pytest.raises(NotImplementedError):
258258
fig.grdimage(grid, A="out.png")
259259

260260

pygmt/tests/test_grdtrack.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pandas as pd
1010
import pytest
1111
from pygmt import grdtrack
12-
from pygmt.exceptions import GMTInvalidInput, GMTParameterError, GMTTypeError
12+
from pygmt.exceptions import GMTParameterError, GMTTypeError
1313
from pygmt.helpers import GMTTempFile
1414
from pygmt.helpers.testing import load_static_earth_relief
1515

@@ -154,7 +154,7 @@ def test_grdtrack_without_outfile_setting(dataarray, dataframe):
154154
"""
155155
Run grdtrack by not passing in outfile parameter setting.
156156
"""
157-
with pytest.raises(GMTInvalidInput):
157+
with pytest.raises(GMTParameterError):
158158
grdtrack(points=dataframe, grid=dataarray)
159159

160160

0 commit comments

Comments
 (0)