Skip to content

Commit 651a7c5

Browse files
seismanweiji14
andauthored
Improve decorators @use_alias/@fmt_docstrings to list non-aliased aliases (#3965)
Co-authored-by: Wei Ji <[email protected]>
1 parent 5eb700a commit 651a7c5

File tree

12 files changed

+88
-3
lines changed

12 files changed

+88
-3
lines changed

pygmt/helpers/decorators.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ def fmt_docstring(module_func):
450450
aliases.append(" :columns: 3\n")
451451
for arg in sorted(module_func.aliases):
452452
alias = module_func.aliases[arg]
453-
aliases.append(f" - {arg} = {alias}")
453+
# Trailing dash means it's not aliased but should be listed.
454+
# Remove the trailing dash if it exists.
455+
aliases.append(f" - {arg} = {alias.rstrip('-')}")
454456
filler_text["aliases"] = "\n".join(aliases)
455457

456458
filler_text["table-classes"] = (
@@ -484,6 +486,9 @@ def _insert_alias(module_func, default_value=None):
484486
kwargs_param = wrapped_params.pop(-1)
485487
# Add new parameters from aliases
486488
for alias in module_func.aliases.values():
489+
if alias.endswith("-"):
490+
# Trailing dash means it's not aliased but should be listed.
491+
continue
487492
if alias not in sig.parameters:
488493
new_param = Parameter(
489494
alias, kind=Parameter.KEYWORD_ONLY, default=default_value
@@ -548,6 +553,31 @@ def new_module(*args, **kwargs):
548553
New module that parses and replaces the registered aliases.
549554
"""
550555
for short_param, long_alias in aliases.items():
556+
if long_alias.endswith("-"):
557+
_long_alias = long_alias.rstrip("-")
558+
# Trailing dash means it's not aliased but should be listed.
559+
_alias_list = _long_alias.split("/")
560+
if (
561+
any(_alias in kwargs for _alias in _alias_list)
562+
and short_param in kwargs
563+
): # Both long- and short- forms are given.
564+
msg = (
565+
f"Parameters in short-form ({short_param}) and "
566+
f"long-form ({_long_alias}) can't coexist."
567+
)
568+
raise GMTInvalidInput(msg)
569+
if short_param in kwargs: # Only short-alias is given
570+
if len(_alias_list) > 1: # Aliased to multiple long-forms
571+
msg = (
572+
f"Short-form parameter ({short_param}) is not "
573+
f"recognized. Use long-form parameter(s) "
574+
f"'{_long_alias}' instead."
575+
)
576+
raise GMTInvalidInput(msg)
577+
# If there is only one long-form parameter, use it.
578+
kwargs[_long_alias] = kwargs.pop(short_param)
579+
continue
580+
551581
if long_alias in kwargs and short_param in kwargs:
552582
msg = (
553583
f"Parameters in short-form ({short_param}) and "

pygmt/src/coast.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
A="area_thresh",
2424
B="frame",
2525
C="lakes",
26+
D="resolution-",
2627
E="dcw",
2728
F="box",
2829
G="land",

pygmt/src/grdclip.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
# TODO(PyGMT>=0.19.0): Remove the deprecated "new" parameter.
2424
@fmt_docstring
2525
@deprecate_parameter("new", "replace", "v0.15.0", remove_version="v0.19.0")
26-
@use_alias(R="region", V="verbose")
26+
@use_alias(
27+
R="region", Sa="above-", Sb="below-", Si="between-", Sr="replace-", V="verbose"
28+
)
2729
@kwargs_to_strings(R="sequence")
2830
def grdclip(
2931
grid: PathLike | xr.DataArray,

pygmt/src/grdfill.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,14 @@ def _parse_fill_mode(
109109
# TODO(PyGMT>=0.19.0): Remove the deprecated 'no_data' parameter.
110110
# TODO(PyGMT>=0.19.0): Remove the deprecated 'mode' parameter.
111111
@deprecate_parameter("no_data", "hole", "v0.15.0", remove_version="v0.19.0")
112-
@use_alias(N="hole", R="region", V="verbose", f="coltypes")
112+
@use_alias(
113+
A="constantfill/gridfill/neighborfill/splinefill/mode-",
114+
L="inquire-",
115+
N="hole",
116+
R="region",
117+
V="verbose",
118+
f="coltypes",
119+
)
113120
@kwargs_to_strings(R="sequence")
114121
def grdfill(
115122
grid: PathLike | xr.DataArray,

pygmt/src/grdlandmask.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@fmt_docstring
1818
@use_alias(
1919
A="area_thresh",
20+
D="resolution-",
2021
E="bordervalues",
2122
I="spacing",
2223
N="maskvalues",

pygmt/src/meca.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def _auto_offset(spec) -> bool:
123123
L="outline",
124124
N="no_clip",
125125
R="region",
126+
S="scale/convention/component-",
126127
T="nodal",
127128
V="verbose",
128129
W="pen",

pygmt/src/select.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
@use_alias(
2525
A="area_thresh",
2626
C="dist2pt",
27+
D="resolution-",
2728
F="polygon",
2829
G="gridmask",
2930
I="reverse",

pygmt/src/solar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
G="fill",
1919
J="projection",
2020
R="region",
21+
T="terminator/terminator_datetime-",
2122
V="verbose",
2223
W="pen",
2324
c="panel",

pygmt/src/ternary.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
B="frame",
1515
C="cmap",
1616
G="fill",
17+
L="alabel/blabel/clabel-",
1718
JX="width",
1819
R="region",
1920
S="style",

pygmt/src/text.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
B="frame",
2828
C="clearance",
2929
D="offset",
30+
F="position/angle/font/justify-",
3031
G="fill",
3132
N="no_clip",
3233
V="verbose",

0 commit comments

Comments
 (0)