Skip to content

Commit 76f7c6e

Browse files
committed
Improve error message for recommended long-form parameters
1 parent 408199c commit 76f7c6e

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

pygmt/alias.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,23 +280,38 @@ def update(self, kwargs: Mapping[str, Any]):
280280
for short_param, value in kwargs.items():
281281
# Long-form parameters exist.
282282
if aliases := self.aliasdict.get(short_param):
283-
long_params = ", ".join(
284-
[repr(alias.name) for alias in aliases]
285-
if isinstance(aliases, Sequence)
286-
else [repr(aliases.name)]
287-
)
283+
if not isinstance(aliases, Sequence): # Single Alias object.
284+
_params = repr(aliases.name)
285+
_msg_long_params = f"Use long-form parameter {_params} instead."
286+
else: # Sequence of Alias objects.
287+
_params = ", ".join(
288+
[repr(alias.name) for alias in aliases if not alias.prefix]
289+
)
290+
_modifiers = ", ".join(
291+
[
292+
repr(alias.name) + f" ({alias.prefix})"
293+
for alias in aliases
294+
if alias.prefix
295+
]
296+
)
297+
_msg_long_params = (
298+
f"Use long-form parameters {_params}, "
299+
f"with optional parameters {_modifiers} instead."
300+
)
301+
288302
# Long-form parameters are already specified.
289303
if short_param in self.kwdict:
290304
msg = (
291-
f"Parameter in short-form {short_param!r} conflicts with "
292-
f"long-form parameter {long_params}."
305+
f"Short-form parameter {short_param!r} conflicts with "
306+
"long-form parameters and is not recommended. "
307+
f"{_msg_long_params}"
293308
)
294309
raise GMTInvalidInput(msg)
295310

296311
# Long-form parameters are not specified.
297312
msg = (
298313
f"Short-form parameter {short_param!r} is not recommended. "
299-
f"Use long-form parameter {long_params} instead."
314+
f"{_msg_long_params}"
300315
)
301316
warnings.warn(msg, category=SyntaxWarning, stacklevel=2)
302317

pygmt/tests/test_alias_system.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_alias_system_one_alias_short_form():
5959
# Coexistence of long-form and short-form parameters.
6060
with pytest.raises(
6161
GMTInvalidInput,
62-
match="Parameter in short-form 'J' conflicts with long-form parameter 'projection'.",
62+
match="Short-form parameter 'J' conflicts with long-form parameters and is not recommended. Use long-form parameter 'projection' instead.",
6363
):
6464
func(projection="X10c", J="H10c")
6565

@@ -70,21 +70,16 @@ def test_alias_system_multiple_aliases_short_form():
7070
are used.
7171
"""
7272
# Long-form exists but is not given, and short-form is given.
73-
with pytest.warns(
74-
SyntaxWarning,
75-
match="Short-form parameter 'U' is not recommended. Use long-form parameter 'label', 'text' instead.",
76-
):
73+
msg = (
74+
r"Short-form parameter 'U' is not recommended. Use long-form parameters 'label', with optional parameters 'text' \(\+t\) instead.",
75+
)
76+
with pytest.warns(SyntaxWarning, match=msg):
7777
assert func(U="abcd+tefg") == ["-Uabcd+tefg"]
7878

7979
# Coexistence of long-form and short-form parameters.
80-
with pytest.raises(
81-
GMTInvalidInput,
82-
match="Parameter in short-form 'U' conflicts with long-form parameter 'label', 'text'.",
83-
):
80+
msg = r"Short-form parameter 'U' conflicts with long-form parameters and is not recommended. Use long-form parameters 'label', with optional parameters 'text' \(\+t\) instead."
81+
with pytest.raises(GMTInvalidInput, match=msg):
8482
func(label="abcd", U="efg")
8583

86-
with pytest.raises(
87-
GMTInvalidInput,
88-
match="Parameter in short-form 'U' conflicts with long-form parameter 'label', 'text'.",
89-
):
84+
with pytest.raises(GMTInvalidInput, match=msg):
9085
func(text="efg", U="efg")

0 commit comments

Comments
 (0)