Skip to content

Commit 9380f5a

Browse files
Chuan1937seisman
andauthored
BREAKING: Raise GMTParameterError exception for as_most_one parameters. Previously raise GMTInvalidInput. Part 2 (#4388)
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
1 parent e9c164a commit 9380f5a

File tree

5 files changed

+23
-31
lines changed

5 files changed

+23
-31
lines changed

pygmt/helpers/decorators.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from inspect import Parameter, signature
1313

1414
import numpy as np
15-
from pygmt.exceptions import GMTInvalidInput, GMTValueError
15+
from pygmt.exceptions import GMTInvalidInput, GMTParameterError, GMTValueError
1616
from pygmt.helpers.utils import is_nonstr_iter
1717

1818
COMMON_DOCSTRINGS = {
@@ -534,8 +534,7 @@ def use_alias(**aliases):
534534
>>> my_module(region="bla", projection="meh", J="bla")
535535
Traceback (most recent call last):
536536
...
537-
pygmt.exceptions.GMTInvalidInput:
538-
Parameters in short-form (J) and long-form (projection) can't coexist.
537+
pygmt.exceptions.GMTParameterError: Mutually exclusive parameters: ...
539538
"""
540539

541540
def alias_decorator(module_func):
@@ -550,11 +549,10 @@ def new_module(*args, **kwargs):
550549
"""
551550
for short_param, long_alias in aliases.items():
552551
if long_alias in kwargs and short_param in kwargs:
553-
msg = (
554-
f"Parameters in short-form ({short_param}) and "
555-
f"long-form ({long_alias}) can't coexist."
552+
raise GMTParameterError(
553+
at_most_one={long_alias, short_param},
554+
reason=f"Long-form parameter {long_alias!r} is recommended.",
556555
)
557-
raise GMTInvalidInput(msg)
558556
if long_alias in kwargs:
559557
kwargs[short_param] = kwargs.pop(long_alias)
560558
elif short_param in kwargs:
@@ -802,9 +800,9 @@ def deprecate_parameter(oldname, newname, deprecate_version, remove_version):
802800
... assert issubclass(w[i].category, FutureWarning)
803801
... assert "deprecated" in str(w[i].message)
804802
data=table.txt, size=5.0, color=red
805-
>>> # using both old and new names will raise an GMTInvalidInput exception
803+
>>> # using both old and new names will raise an GMTParameterError exception
806804
>>> import pytest
807-
>>> with pytest.raises(GMTInvalidInput):
805+
>>> with pytest.raises(GMTParameterError):
808806
... module(data="table.txt", size=5.0, sizes=4.0)
809807
"""
810808

@@ -821,8 +819,10 @@ def new_module(*args, **kwargs):
821819
"""
822820
if oldname in kwargs:
823821
if newname in kwargs:
824-
msg = f"Can't provide both '{newname}' and '{oldname}'."
825-
raise GMTInvalidInput(msg)
822+
raise GMTParameterError(
823+
at_most_one={newname, oldname},
824+
reason=f"{oldname!r} is deprecated and {newname!r} is recommended.",
825+
)
826826
msg = (
827827
f"The '{oldname}' parameter has been deprecated since {deprecate_version}"
828828
f" and will be removed in {remove_version}."

pygmt/src/project.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pygmt._typing import PathLike, TableLike
1111
from pygmt.alias import Alias, AliasSystem
1212
from pygmt.clib import Session
13-
from pygmt.exceptions import GMTInvalidInput, GMTParameterError
13+
from pygmt.exceptions import GMTParameterError
1414
from pygmt.helpers import (
1515
build_arg_list,
1616
fmt_docstring,
@@ -227,8 +227,7 @@ def project( # noqa: PLR0913
227227
required="data", reason="Required unless 'generate' is set."
228228
)
229229
if kwargs.get("G") is not None and kwargs.get("F") is not None:
230-
msg = "The 'convention' parameter is not allowed with 'generate'."
231-
raise GMTInvalidInput(msg)
230+
raise GMTParameterError(at_most_one={"convention", "generate"})
232231

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

pygmt/src/text.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pygmt._typing import AnchorCode, PathLike, StringArrayTypes, TableLike
1010
from pygmt.alias import Alias, AliasSystem
1111
from pygmt.clib import Session
12-
from pygmt.exceptions import GMTInvalidInput, GMTParameterError, GMTTypeError
12+
from pygmt.exceptions import GMTParameterError, GMTTypeError
1313
from pygmt.helpers import (
1414
_check_encoding,
1515
build_arg_list,
@@ -34,7 +34,7 @@
3434
it="use_word",
3535
w="wrap",
3636
)
37-
def text_( # noqa: PLR0912, PLR0913, PLR0915
37+
def text_( # noqa: PLR0912, PLR0913
3838
self,
3939
textfiles: PathLike | TableLike | None = None,
4040
x=None,
@@ -191,8 +191,7 @@ def text_( # noqa: PLR0912, PLR0913, PLR0915
191191
+ (position is not None)
192192
+ (x is not None or y is not None)
193193
) != 1:
194-
msg = "Provide either 'textfiles', 'x'/'y'/'text', or 'position'/'text'."
195-
raise GMTInvalidInput(msg)
194+
raise GMTParameterError(at_most_one={"textfiles", "position/text", "x/y/text"})
196195

197196
data_is_required = position is None
198197
kind = data_kind(textfiles, required=data_is_required)
@@ -209,8 +208,7 @@ def text_( # noqa: PLR0912, PLR0913, PLR0915
209208
)
210209

211210
if textfiles is not None and text is not None:
212-
msg = "'text' can't be specified when 'textfiles' is given."
213-
raise GMTInvalidInput(msg)
211+
raise GMTParameterError(at_most_one={"text", "textfiles"})
214212
if kind == "empty" and text is None:
215213
raise GMTParameterError(
216214
required="text", reason="Required when 'x' and 'y' are set."

pygmt/tests/test_project.py

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

1616

@@ -87,6 +87,6 @@ def test_project_incorrect_parameters():
8787
with pytest.raises(GMTParameterError):
8888
# No `data` or `generate`
8989
project(center=[0, -1], azimuth=45, flat_earth=True)
90-
with pytest.raises(GMTInvalidInput):
90+
with pytest.raises(GMTParameterError):
9191
# Using `generate` with `convention`
9292
project(center=[0, -1], generate=0.5, convention="xypqrsz")

pygmt/tests/test_text.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
import numpy as np
88
import pytest
99
from pygmt import Figure, config
10-
from pygmt.exceptions import (
11-
GMTCLibError,
12-
GMTInvalidInput,
13-
GMTParameterError,
14-
GMTTypeError,
15-
)
10+
from pygmt.exceptions import GMTCLibError, GMTParameterError, GMTTypeError
1611
from pygmt.helpers import GMTTempFile
1712
from pygmt.helpers.testing import skip_if_no
1813

@@ -151,19 +146,19 @@ def test_text_invalid_inputs(region):
151146
Run text by providing invalid combinations of inputs.
152147
"""
153148
fig = Figure()
154-
with pytest.raises(GMTInvalidInput):
149+
with pytest.raises(GMTParameterError):
155150
fig.text(
156151
region=region, projection="x1c", x=1.2, y=2.4, position="MC", text="text"
157152
)
158-
with pytest.raises(GMTInvalidInput):
153+
with pytest.raises(GMTParameterError):
159154
fig.text(region=region, projection="x1c", textfiles="file.txt", text="text")
160155
with pytest.raises(GMTParameterError):
161156
fig.text(region=region, projection="x1c", position="MC", text=None)
162157
with pytest.raises(GMTTypeError):
163158
fig.text(
164159
region=region, projection="x1c", position="MC", text=["text1", "text2"]
165160
)
166-
with pytest.raises(GMTInvalidInput):
161+
with pytest.raises(GMTParameterError):
167162
fig.text(region=region, projection="x1c", textfiles="file.txt", x=1.2, y=2.4)
168163

169164

0 commit comments

Comments
 (0)