Skip to content

Commit d553a25

Browse files
committed
Use GMTParameterError for missing required parameters
1 parent 19d3df2 commit d553a25

17 files changed

+41
-48
lines changed

pygmt/src/filter1d.py

Lines changed: 2 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
11+
from pygmt.exceptions import GMTParameterError
1212
from pygmt.helpers import (
1313
build_arg_list,
1414
fmt_docstring,
@@ -112,8 +112,7 @@ def filter1d(
112112
(depends on ``output_type``)
113113
"""
114114
if kwargs.get("F") is None:
115-
msg = "Pass a required argument to 'filter_type'."
116-
raise GMTInvalidInput(msg)
115+
raise GMTParameterError(required={"filter_type"})
117116

118117
output_type = validate_output_table_type(output_type, outfile=outfile)
119118

pygmt/src/grdlandmask.py

Lines changed: 2 additions & 3 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
11+
from pygmt.exceptions import GMTParameterError
1212
from pygmt.helpers import (
1313
build_arg_list,
1414
fmt_docstring,
@@ -115,8 +115,7 @@ def grdlandmask(
115115
>>> landmask = pygmt.grdlandmask(spacing=1, region=[125, 130, 30, 35])
116116
"""
117117
if kwargs.get("I") is None or kwargs.get("R") is None:
118-
msg = "Both 'region' and 'spacing' must be specified."
119-
raise GMTInvalidInput(msg)
118+
raise GMTParameterError(required={"spacing", "region"})
120119

121120
kwargs["D"] = kwargs.get("D", _parse_coastline_resolution(resolution))
122121
kwargs["N"] = sequence_join(maskvalues, size=(2, 5), name="maskvalues")

pygmt/src/grdproject.py

Lines changed: 2 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
8+
from pygmt.exceptions import GMTParameterError
99
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
1010

1111
__doctest_skip__ = ["grdproject"]
@@ -106,8 +106,7 @@ def grdproject(
106106
>>> new_grid = pygmt.grdproject(grid=grid, projection="M10c", region=region)
107107
"""
108108
if kwargs.get("J") is None:
109-
msg = "The projection must be specified."
110-
raise GMTInvalidInput(msg)
109+
raise GMTParameterError(required={"projection"})
111110

112111
with Session() as lib:
113112
with (

pygmt/src/grdtrack.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import xarray as xr
1010
from pygmt._typing import PathLike, TableLike
1111
from pygmt.clib import Session
12-
from pygmt.exceptions import GMTInvalidInput
12+
from pygmt.exceptions import GMTInvalidInput, GMTParameterError
1313
from pygmt.helpers import (
1414
build_arg_list,
1515
fmt_docstring,
@@ -300,8 +300,10 @@ def grdtrack(
300300
raise GMTInvalidInput(msg)
301301

302302
if hasattr(points, "columns") and newcolname is None:
303-
msg = "Please pass in a str to 'newcolname'."
304-
raise GMTInvalidInput(msg)
303+
raise GMTParameterError(
304+
required={"newcolname"},
305+
reason="Parameter 'newcolname' is required when 'points' is a pandas.DataFrame object.",
306+
)
305307

306308
output_type = validate_output_table_type(output_type, outfile=outfile)
307309

pygmt/src/meca.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pandas as pd
1010
from pygmt._typing import PathLike, TableLike
1111
from pygmt.clib import Session
12-
from pygmt.exceptions import GMTInvalidInput, GMTValueError
12+
from pygmt.exceptions import GMTParameterError, GMTValueError
1313
from pygmt.helpers import (
1414
build_arg_list,
1515
data_kind,
@@ -30,8 +30,7 @@ def _get_focal_convention(spec, convention, component) -> _FocalMechanismConvent
3030

3131
# Determine the convention from the 'convention' parameter.
3232
if convention is None:
33-
msg = "Parameter 'convention' must be specified."
34-
raise GMTInvalidInput(msg)
33+
raise GMTParameterError(required={"convention"})
3534
return _FocalMechanismConvention(convention=convention, component=component)
3635

3736

pygmt/src/project.py

Lines changed: 2 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
11+
from pygmt.exceptions import GMTInvalidInput, GMTParameterError
1212
from pygmt.helpers import (
1313
build_arg_list,
1414
fmt_docstring,
@@ -223,8 +223,7 @@ def project(
223223
(depends on ``output_type``)
224224
"""
225225
if kwargs.get("C") is None:
226-
msg = "The 'center' parameter must be specified."
227-
raise GMTInvalidInput(msg)
226+
raise GMTParameterError(required={"center"})
228227
if kwargs.get("G") is None and data is None:
229228
msg = "The 'data' parameter must be specified unless 'generate' is used."
230229
raise GMTInvalidInput(msg)

pygmt/src/sphdistance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import xarray as xr
77
from pygmt._typing import PathLike, TableLike
88
from pygmt.clib import Session
9-
from pygmt.exceptions import GMTInvalidInput
9+
from pygmt.exceptions import GMTParameterError
1010
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
1111

1212
__doctest_skip__ = ["sphdistance"]
@@ -115,8 +115,8 @@ def sphdistance(
115115
... )
116116
"""
117117
if kwargs.get("I") is None or kwargs.get("R") is None:
118-
msg = "Both 'region' and 'spacing' must be specified."
119-
raise GMTInvalidInput(msg)
118+
raise GMTParameterError(required={"spacing", "region"})
119+
120120
with Session() as lib:
121121
with (
122122
lib.virtualfile_in(check_kind="vector", data=data, x=x, y=y) as vintbl,

pygmt/src/velo.py

Lines changed: 3 additions & 6 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, GMTTypeError
9+
from pygmt.exceptions import GMTParameterError, GMTTypeError
1010
from pygmt.helpers import (
1111
build_arg_list,
1212
fmt_docstring,
@@ -241,11 +241,8 @@ def velo(self, data: PathLike | TableLike | None = None, **kwargs):
241241
"""
242242
self._activate_figure()
243243

244-
if kwargs.get("S") is None or (
245-
kwargs.get("S") is not None and not isinstance(kwargs["S"], str)
246-
):
247-
msg = "The parameter 'spec' is required and has to be a string."
248-
raise GMTInvalidInput(msg)
244+
if kwargs.get("S") is None:
245+
raise GMTParameterError(required={"spec"})
249246

250247
if isinstance(data, np.ndarray) and not pd.api.types.is_numeric_dtype(data):
251248
raise GMTTypeError(

pygmt/src/xyz2grd.py

Lines changed: 2 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, TableLike
77
from pygmt.clib import Session
8-
from pygmt.exceptions import GMTInvalidInput
8+
from pygmt.exceptions import GMTParameterError
99
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
1010

1111
__doctest_skip__ = ["xyz2grd"]
@@ -149,8 +149,7 @@ def xyz2grd(
149149
... )
150150
"""
151151
if kwargs.get("I") is None or kwargs.get("R") is None:
152-
msg = "Both 'region' and 'spacing' must be specified."
153-
raise GMTInvalidInput(msg)
152+
raise GMTParameterError(required={"spacing", "region"})
154153

155154
with Session() as lib:
156155
with (

pygmt/tests/test_grdlandmask.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 grdlandmask
1010
from pygmt.enums import GridRegistration, GridType
11-
from pygmt.exceptions import GMTInvalidInput
11+
from pygmt.exceptions import GMTParameterError
1212
from pygmt.helpers import GMTTempFile
1313

1414

@@ -64,5 +64,5 @@ def test_grdlandmask_fails():
6464
"""
6565
Check that grdlandmask fails correctly when region and spacing are not given.
6666
"""
67-
with pytest.raises(GMTInvalidInput):
67+
with pytest.raises(GMTParameterError):
6868
grdlandmask()

0 commit comments

Comments
 (0)