Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support tab completion in Jupyter by inserting aliases into the method signature #1282

Merged
merged 14 commits into from
Jun 16, 2021
Merged
Prev Previous commit
Next Next commit
Call insert_alias from inside use_alias
  • Loading branch information
maxrjones committed May 23, 2021
commit dc2570760c09b6480e7072036ba8d344cd143699
50 changes: 26 additions & 24 deletions pygmt/helpers/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,30 @@ def fmt_docstring(module_func):
return module_func


def insert_alias(module_func):
"""
Decorator insertings aliases into the signature of a method.
"""

# Get current signature and parameters
sig = signature(module_func)
wrapped_params = list(sig.parameters.values())
kwargs_param = wrapped_params.pop(-1)
# Add new parameters from aliases
for alias in module_func.aliases.values():
if alias not in sig.parameters.keys():
new_param = Parameter(
alias, kind=Parameter.POSITIONAL_OR_KEYWORD, default=None
)
wrapped_params = wrapped_params + [new_param]
all_params = wrapped_params + [kwargs_param]
# Update method signature
sig_new = sig.replace(parameters=all_params)
module_func.__signature__ = sig_new

return module_func


def use_alias(**aliases):
"""
Decorator to add aliases to keyword arguments of a function.
Expand Down Expand Up @@ -323,35 +347,13 @@ def new_module(*args, **kwargs):

new_module.aliases = aliases

new_module = insert_alias(new_module)
maxrjones marked this conversation as resolved.
Show resolved Hide resolved

return new_module

return alias_decorator


def insert_alias(module_func):
"""
Decorator insertings aliases into the signature of a method.
"""

# Get current signature and parameters
sig = signature(module_func)
wrapped_params = list(sig.parameters.values())
kwargs_param = wrapped_params.pop(-1)
# Add new parameters from aliases
for alias in module_func.aliases.values():
if alias not in sig.parameters.keys():
new_param = Parameter(
alias, kind=Parameter.POSITIONAL_OR_KEYWORD, default=None
)
wrapped_params = wrapped_params + [new_param]
all_params = wrapped_params + [kwargs_param]
# Update method signature
sig_new = sig.replace(parameters=all_params)
module_func.__signature__ = sig_new

return module_func


def kwargs_to_strings(**conversions):
"""
Decorator to convert given keyword arguments to strings.
Expand Down
3 changes: 0 additions & 3 deletions pygmt/src/blockm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
data_kind,
dummy_context,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)
Expand Down Expand Up @@ -72,7 +71,6 @@ def _blockm(block_method, table, outfile, **kwargs):


@fmt_docstring
@insert_alias
@use_alias(
I="spacing",
R="region",
Expand Down Expand Up @@ -134,7 +132,6 @@ def blockmean(table, outfile=None, **kwargs):


@fmt_docstring
@insert_alias
@use_alias(
I="spacing",
R="region",
Expand Down