Skip to content

Commit 5259b1e

Browse files
committed
Simplify the logic of the AliasSystem.merge() method
1 parent 99845c2 commit 5259b1e

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

pygmt/alias.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -277,36 +277,40 @@ def merge(self, kwargs: Mapping[str, Any]):
277277
"""
278278
# Loop over short-form parameters passed via kwargs.
279279
for short_param, value in kwargs.items():
280+
# Check if long-form parameters exist and given.
281+
long_param_exists = short_param in self.aliasdict
282+
long_param_given = short_param in self
283+
284+
# Update the dictionary with the short-form parameter anyway.
285+
self[short_param] = value
286+
287+
# Long-form parameters do not exist.
288+
if not long_param_exists:
289+
continue
290+
280291
# Long-form parameters exist.
281-
if aliases := self.aliasdict.get(short_param):
282-
if not isinstance(aliases, Sequence): # Single Alias object.
283-
_msg_long = f"Use long-form parameter {aliases.name!r} instead."
284-
else: # Sequence of Alias objects.
285-
_params = [f"{v.name!r}" for v in aliases if not v.prefix]
286-
_modifiers = [
287-
f"{v.name!r} ({v.prefix})" for v in aliases if v.prefix
288-
]
289-
_msg_long = (
290-
f"Use long-form parameters {', '.join(_params)}, "
291-
f"with optional parameters {', '.join(_modifiers)} instead."
292-
)
293-
294-
# Long-form parameters are already specified.
295-
if short_param in self:
296-
msg = (
297-
f"Short-form parameter {short_param!r} conflicts with "
298-
"long-form parameters and is not recommended. "
299-
f"{_msg_long}"
300-
)
301-
raise GMTInvalidInput(msg)
302-
303-
# Long-form parameters are not specified.
292+
aliases = self.aliasdict.get(short_param)
293+
if not isinstance(aliases, Sequence): # Single Alias object.
294+
_msg_long = f"Use long-form parameter {aliases.name!r} instead."
295+
else: # Sequence of Alias objects.
296+
_params = [f"{v.name!r}" for v in aliases if not v.prefix]
297+
_modifiers = [f"{v.name!r} ({v.prefix})" for v in aliases if v.prefix]
298+
_msg_long = (
299+
f"Use long-form parameters {', '.join(_params)}, "
300+
f"with optional parameters {', '.join(_modifiers)} instead."
301+
)
302+
303+
# Long-form parameters are already specified.
304+
if long_param_given:
304305
msg = (
305-
f"Short-form parameter {short_param!r} is not recommended. "
306-
f"{_msg_long}"
306+
f"Short-form parameter {short_param!r} conflicts with long-form "
307+
f"parameters and is not recommended. {_msg_long}"
307308
)
308-
warnings.warn(msg, category=SyntaxWarning, stacklevel=2)
309+
raise GMTInvalidInput(msg)
309310

310-
# Update the dictionary with the short-form parameter anyway.
311-
self[short_param] = value
311+
# Long-form parameters are not specified.
312+
msg = (
313+
f"Short-form parameter {short_param!r} is not recommended. {_msg_long}"
314+
)
315+
warnings.warn(msg, category=SyntaxWarning, stacklevel=2)
312316
return self

0 commit comments

Comments
 (0)