Skip to content

Commit e51bb31

Browse files
committed
Use GMTParameterError for parameters where at least one is required
1 parent 11fd4a0 commit e51bb31

File tree

11 files changed

+38
-35
lines changed

11 files changed

+38
-35
lines changed

pygmt/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class GMTParameterError(GMTError):
140140
----------
141141
required
142142
Names of required parameters.
143+
require_any
144+
Names of parameters where at least one must be specified.
143145
exclusive
144146
Names of mutually exclusive parameters.
145147
reason
@@ -150,6 +152,7 @@ def __init__(
150152
self,
151153
*,
152154
required: Set[str] | None = None,
155+
require_any: Set[str] | None = None,
153156
exclusive: Set[str] | None = None,
154157
reason: str | None = None,
155158
):
@@ -159,6 +162,11 @@ def __init__(
159162
"Required parameter(s) are missing: "
160163
f"{', '.join(repr(par) for par in required)}."
161164
)
165+
if require_any:
166+
msg = (
167+
"At least one of the following parameters must be specified: "
168+
f"{', '.join(repr(par) for par in require_any)}."
169+
)
162170
if exclusive:
163171
msg = (
164172
"Mutually exclusive parameter(s) are specified: "

pygmt/src/coast.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Literal
66

77
from pygmt.clib import Session
8-
from pygmt.exceptions import GMTInvalidInput
8+
from pygmt.exceptions import GMTParameterError
99
from pygmt.helpers import (
1010
args_in_kwargs,
1111
build_arg_list,
@@ -206,11 +206,18 @@ def coast(
206206
"""
207207
self._activate_figure()
208208
if not args_in_kwargs(args=["C", "G", "S", "I", "N", "E", "Q", "W"], kwargs=kwargs):
209-
msg = (
210-
"At least one of the following parameters must be specified: "
211-
"lakes, land, water, rivers, borders, dcw, Q, or shorelines."
209+
raise GMTParameterError(
210+
require_any={
211+
"lakes",
212+
"land",
213+
"water",
214+
"rivers",
215+
"borders",
216+
"dcw",
217+
"Q",
218+
"shorelines",
219+
}
212220
)
213-
raise GMTInvalidInput(msg)
214221

215222
kwargs["D"] = kwargs.get("D", _parse_coastline_resolution(resolution))
216223

pygmt/src/dimfilter.py

Lines changed: 2 additions & 6 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__ = ["dimfilter"]
@@ -138,11 +138,7 @@ def dimfilter(
138138
... )
139139
"""
140140
if not all(arg in kwargs for arg in ["D", "F", "N"]) and "Q" not in kwargs:
141-
msg = (
142-
"At least one of the following parameters must be specified: "
143-
"distance, filters, or sectors."
144-
)
145-
raise GMTInvalidInput(msg)
141+
raise GMTParameterError(require_any={"distance", "filter", "sectors"})
146142
with Session() as lib:
147143
with (
148144
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,

pygmt/src/grdclip.py

Lines changed: 2 additions & 6 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
10+
from pygmt.exceptions import GMTParameterError
1111
from pygmt.helpers import (
1212
build_arg_list,
1313
deprecate_parameter,
@@ -107,11 +107,7 @@ def grdclip(
107107
[0.0, 10000.0]
108108
"""
109109
if all(v is None for v in (above, below, between, replace)):
110-
msg = (
111-
"Must specify at least one of the following parameters: ",
112-
"'above', 'below', 'between', or 'replace'.",
113-
)
114-
raise GMTInvalidInput(msg)
110+
raise GMTParameterError(require_any={"above", "below", "between", "replace"})
115111

116112
# Parse the -S option.
117113
kwargs["Sa"] = sequence_join(above, size=2, name="above")

pygmt/src/grdgradient.py

Lines changed: 3 additions & 6 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 GMTInvalidInput, GMTParameterError
99
from pygmt.helpers import (
1010
args_in_kwargs,
1111
build_arg_list,
@@ -165,11 +165,8 @@ def grdgradient(
165165
msg = "Must specify normalize if tiles is specified."
166166
raise GMTInvalidInput(msg)
167167
if not args_in_kwargs(args=["A", "D", "E"], kwargs=kwargs):
168-
msg = (
169-
"At least one of the following parameters must be specified: "
170-
"azimuth, direction, or radiance."
171-
)
172-
raise GMTInvalidInput(msg)
168+
raise GMTParameterError(require_any={"azimuth", "direction", "radiance"})
169+
173170
with Session() as lib:
174171
with (
175172
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,

pygmt/src/grdtrack.py

Lines changed: 2 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, GMTParameterError
12+
from pygmt.exceptions import GMTParameterError
1313
from pygmt.helpers import (
1414
build_arg_list,
1515
fmt_docstring,
@@ -295,8 +295,7 @@ def grdtrack(
295295
raise GMTParameterError(exclusive={"points", "profile"})
296296

297297
if points is None and kwargs.get("E") is None:
298-
msg = "Must give 'points' or set 'profile'."
299-
raise GMTInvalidInput(msg)
298+
raise GMTParameterError(require_any={"points", "profile"})
300299

301300
if hasattr(points, "columns") and newcolname is None:
302301
raise GMTParameterError(

pygmt/tests/test_coast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66
from pygmt import Figure
7-
from pygmt.exceptions import GMTInvalidInput
7+
from pygmt.exceptions import GMTInvalidInput, GMTParameterError
88

99

1010
@pytest.mark.benchmark
@@ -40,7 +40,7 @@ def test_coast_required_args():
4040
Test if fig.coast fails when not given required arguments.
4141
"""
4242
fig = Figure()
43-
with pytest.raises(GMTInvalidInput):
43+
with pytest.raises(GMTParameterError):
4444
fig.coast(region="EG")
4545

4646

pygmt/tests/test_dimfilter.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 dimfilter
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
from pygmt.helpers.testing import load_static_earth_relief
1414

@@ -80,5 +80,5 @@ def test_dimfilter_fails(grid):
8080
Check that dimfilter fails correctly when not all of sectors, filters, and distance
8181
are specified.
8282
"""
83-
with pytest.raises(GMTInvalidInput):
83+
with pytest.raises(GMTParameterError):
8484
dimfilter(grid=grid, sectors="l6", distance=4)

pygmt/tests/test_grdclip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pygmt import grdclip
1212
from pygmt.datasets import load_earth_mask
1313
from pygmt.enums import GridRegistration, GridType
14-
from pygmt.exceptions import GMTInvalidInput
14+
from pygmt.exceptions import GMTParameterError
1515
from pygmt.helpers import GMTTempFile
1616
from pygmt.helpers.testing import load_static_earth_relief
1717

@@ -110,5 +110,5 @@ def test_grdclip_missing_required_parameter(grid):
110110
"""
111111
Test that grdclip raises a ValueError if the required parameter is missing.
112112
"""
113-
with pytest.raises(GMTInvalidInput):
113+
with pytest.raises(GMTParameterError):
114114
grdclip(grid=grid)

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
11+
from pygmt.exceptions import GMTInvalidInput, GMTParameterError
1212
from pygmt.helpers import GMTTempFile
1313
from pygmt.helpers.testing import load_static_earth_relief
1414

@@ -80,7 +80,7 @@ def test_grdgradient_fails(grid):
8080
Check that grdgradient fails correctly when `tiles` is specified but
8181
normalize is not.
8282
"""
83-
with pytest.raises(GMTInvalidInput):
83+
with pytest.raises(GMTParameterError):
8484
grdgradient(grid=grid) # fails without required arguments
8585
with pytest.raises(GMTInvalidInput):
8686
# fails when tiles is specified but not normalize

0 commit comments

Comments
 (0)