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
Update decorator to work with all aliases
  • Loading branch information
maxrjones committed May 21, 2021
commit 49aa7b5b70a5dee7e3283db6a3c93535f3bb2b23
17 changes: 12 additions & 5 deletions pygmt/helpers/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import functools
import textwrap
import warnings
from inspect import Parameter, signature

import numpy as np
from inspect import signature, Parameter
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers.utils import is_nonstr_iter

Expand Down Expand Up @@ -328,17 +328,24 @@ def new_module(*args, **kwargs):
return alias_decorator



def tab_complete_alias(module_func):
"""
Decorator injecting aliases of a method as attributes
Decorator injecting aliases into the signature of a method.
"""

# Get current signature and parameters
sig = signature(module_func)
param = Parameter("verbose",kind=Parameter.POSITIONAL_OR_KEYWORD,default=None)
wrapped_params = [param for param in sig.parameters.values()]
kwargs_param = wrapped_params.pop(-1)
all_params = wrapped_params + [param] + [kwargs_param]
# 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

Expand Down