Skip to content

Commit cc89cb1

Browse files
committed
Use GMTParameteError for mutally exclusive parameters
1 parent e048934 commit cc89cb1

File tree

12 files changed

+24
-30
lines changed

12 files changed

+24
-30
lines changed

pygmt/src/grd2cpt.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__ = ["grd2cpt"]
@@ -184,8 +184,7 @@ def grd2cpt(grid: PathLike | xr.DataArray, **kwargs):
184184
>>> fig.show()
185185
"""
186186
if kwargs.get("W") is not None and kwargs.get("Ww") is not None:
187-
msg = "Set only 'categorical' or 'cyclic' to True, not both."
188-
raise GMTInvalidInput(msg)
187+
raise GMTParameterError(exclusive={"categorical", "cyclic"})
189188

190189
if (output := kwargs.pop("H", None)) is not None:
191190
kwargs["H"] = True

pygmt/src/grdfill.py

Lines changed: 6 additions & 7 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 GMTInvalidInput, GMTParameterError
1212
from pygmt.helpers import (
1313
build_arg_list,
1414
deprecate_parameter,
@@ -37,22 +37,22 @@ def _validate_params(
3737
>>> _validate_params(constantfill=20.0, gridfill="bggrid.nc")
3838
Traceback (most recent call last):
3939
...
40-
pygmt.exceptions.GMTInvalidInput: Parameters ... are mutually exclusive.
40+
pygmt.exceptions.GMTParameterError: Mutually exclusive parameter...
4141
>>> _validate_params(constantfill=20.0, inquire=True)
4242
Traceback (most recent call last):
4343
...
44-
pygmt.exceptions.GMTInvalidInput: Parameters ... are mutually exclusive.
44+
pygmt.exceptions.GMTParameterError: Mutually exclusive parameter...
4545
>>> _validate_params()
4646
Traceback (most recent call last):
4747
...
4848
pygmt.exceptions.GMTInvalidInput: Need to specify parameter ...
4949
"""
50-
_fill_params = "'constantfill'/'gridfill'/'neighborfill'/'splinefill'"
50+
_fill_params = {"constantfill", "gridfill", "neighborfill", "splinefill"}
5151
# The deprecated 'mode' parameter is given.
5252
if mode is not None:
5353
msg = (
5454
"The 'mode' parameter is deprecated since v0.15.0 and will be removed in "
55-
f"v0.19.0. Use {_fill_params} instead."
55+
f"v0.19.0. Use {', '.join(repr(par) for par in _fill_params)} instead."
5656
)
5757
warnings.warn(msg, FutureWarning, stacklevel=2)
5858

@@ -61,8 +61,7 @@ def _validate_params(
6161
for param in [constantfill, gridfill, neighborfill, splinefill, inquire, mode]
6262
)
6363
if n_given > 1: # More than one mutually exclusive parameter is given.
64-
msg = f"Parameters {_fill_params}/'inquire'/'mode' are mutually exclusive."
65-
raise GMTInvalidInput(msg)
64+
raise GMTParameterError(exclusive=[*_fill_params, "inquire", "mode"])
6665
if n_given == 0: # No parameters are given.
6766
msg = (
6867
f"Need to specify parameter {_fill_params} for filling holes or "

pygmt/src/grdtrack.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ def grdtrack(
292292
... )
293293
"""
294294
if points is not None and kwargs.get("E") is not None:
295-
msg = "Can't set both 'points' and 'profile'."
296-
raise GMTInvalidInput(msg)
295+
raise GMTParameterError(exclusive=("points", "profile"))
297296

298297
if points is None and kwargs.get("E") is None:
299298
msg = "Must give 'points' or set 'profile'."

pygmt/src/makecpt.py

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

55
from pygmt.clib import Session
6-
from pygmt.exceptions import GMTInvalidInput
6+
from pygmt.exceptions import GMTParameterError
77
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
88

99

@@ -156,8 +156,7 @@ def makecpt(**kwargs):
156156
``categorical=True``.
157157
"""
158158
if kwargs.get("W") is not None and kwargs.get("Ww") is not None:
159-
msg = "Set only categorical or cyclic to True, not both."
160-
raise GMTInvalidInput(msg)
159+
raise GMTParameterError(exclusive={"categorical", "cyclic"})
161160

162161
if (output := kwargs.pop("H", None)) is not None:
163162
kwargs["H"] = True

pygmt/src/project.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,7 @@ def project(
228228
msg = "The 'data' parameter must be specified unless 'generate' is used."
229229
raise GMTInvalidInput(msg)
230230
if kwargs.get("G") is not None and kwargs.get("F") is not None:
231-
msg = "The 'convention' parameter is not allowed with 'generate'."
232-
raise GMTInvalidInput(msg)
231+
raise GMTParameterError(exclusive={"generate", "convention"})
233232

234233
output_type = validate_output_table_type(output_type, outfile=outfile)
235234

pygmt/src/subplot.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import contextlib
66

77
from pygmt.clib import Session
8-
from pygmt.exceptions import GMTInvalidInput, GMTValueError
8+
from pygmt.exceptions import GMTParameterError, GMTValueError
99
from pygmt.helpers import (
1010
build_arg_list,
1111
fmt_docstring,
@@ -156,8 +156,7 @@ def subplot(self, nrows=1, ncols=1, **kwargs):
156156
)
157157

158158
if kwargs.get("Ff") and kwargs.get("Fs"):
159-
msg = "Please provide either one of 'figsize' or 'subsize' only."
160-
raise GMTInvalidInput(msg)
159+
raise GMTParameterError(exclusive={"figsize", "subsize"})
161160

162161
# Need to use separate sessions for "subplot begin" and "subplot end".
163162
# Otherwise, "subplot end" will use the last session, which may cause

pygmt/tests/test_grd2cpt.py

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

77
import pytest
88
from pygmt import Figure, grd2cpt
9-
from pygmt.exceptions import GMTInvalidInput, GMTTypeError, GMTValueError
9+
from pygmt.exceptions import GMTParameterError, GMTTypeError, GMTValueError
1010
from pygmt.helpers import GMTTempFile
1111
from pygmt.helpers.testing import load_static_earth_relief
1212

@@ -70,5 +70,5 @@ def test_grd2cpt_categorical_and_cyclic(grid):
7070
"""
7171
Use incorrect setting by setting both categorical and cyclic to True.
7272
"""
73-
with pytest.raises(GMTInvalidInput):
73+
with pytest.raises(GMTParameterError):
7474
grd2cpt(grid=grid, cmap="batlow", categorical=True, cyclic=True)

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
13+
from pygmt.exceptions import GMTInvalidInput, GMTParameterError
1414
from pygmt.helpers import GMTTempFile
1515
from pygmt.helpers.testing import load_static_earth_relief
1616

@@ -136,7 +136,7 @@ def test_grdfill_inquire_and_fill(grid):
136136
"""
137137
Test that grdfill fails if both inquire and fill parameters are given.
138138
"""
139-
with pytest.raises(GMTInvalidInput):
139+
with pytest.raises(GMTParameterError):
140140
grdfill(grid=grid, inquire=True, constantfill=20)
141141

142142

pygmt/tests/test_grdtrack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,5 @@ def test_grdtrack_set_points_and_profile(dataarray, dataframe):
170170
"""
171171
Run grdtrack but set both 'points' and 'profile'.
172172
"""
173-
with pytest.raises(GMTInvalidInput):
173+
with pytest.raises(GMTParameterError):
174174
grdtrack(grid=dataarray, points=dataframe, profile="BL/TR")

pygmt/tests/test_makecpt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88
import pytest
99
from pygmt import Figure, makecpt
10-
from pygmt.exceptions import GMTInvalidInput, GMTValueError
10+
from pygmt.exceptions import GMTParameterError, GMTValueError
1111
from pygmt.helpers import GMTTempFile
1212

1313
POINTS_DATA = Path(__file__).parent / "data" / "points.txt"
@@ -166,5 +166,5 @@ def test_makecpt_categorical_and_cyclic():
166166
"""
167167
Use incorrect setting by setting both categorical and cyclic to True.
168168
"""
169-
with pytest.raises(GMTInvalidInput):
169+
with pytest.raises(GMTParameterError):
170170
makecpt(cmap="batlow", categorical=True, cyclic=True)

0 commit comments

Comments
 (0)