-
Notifications
You must be signed in to change notification settings - Fork 224
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
Conversation
Great work! For me, it works well in IPython console, Jupyter notebook, but not in VSCode. |
pygmt/helpers/decorators.py
Outdated
@@ -327,6 +328,30 @@ def new_module(*args, **kwargs): | |||
return alias_decorator | |||
|
|||
|
|||
def insert_alias(module_func): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should move the code into the use_alias
decorator, because we only need to "insert aliases into the signature" when aliases are defined by @use_alias
? It also means all functions support tab completion without any further actions (i.e., adding @insert_alias
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that combinging use_alias
and insert_alias
is the smart move, since I don't see how insert_alias
would be used on its own.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of implementing this inside use_alias
to simplify the diff, but would prefer to not combine by simply moving the code inside use_alias
. As an alternate solution, my latest commit calls insert_alias
from within use_alias
. There are two reasons why I prefer this option to having all the code within use_alias
. First, I think that it is simpler (i.e., easier to maintain) if individual functions do smaller, isolated tasks. Second, this implementation is compatible with the changes proposed in #1282. Even if that PR doesn't get merged, it seems to me that having insert_alias
as a separate function could be more compatible with future updates to the decorators (reason 1).
This reverts commit d3a046e.
I learned that parameter order matters for tab completion when trying this out. For example, I can tab complete each parameter in this use of basemap:
but cannot tab complete region if I select the projection parameter first:
It seems like this is the same for other libraries in Jupyter notebooks (e.g., numpy), so I am not sure if there is a way around this but will look into it more. If not, it motivates ordering the aliases in some way that reflects a likely usage order as much as possible. |
Is it related to the |
Yes, thanks. I went with the default setting of Actually, it might be worth considering setting other parameters to
But
Or similarly only supporting keyword arguments for |
One weakness to the current implementation is that all parameters defined by |
But any parameter with a value of |
You are correct. I had thought that required arguments should not have a default |
Co-authored-by: Wei Ji <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great improvement!
Made a small change to the title to clarify that tab completion only works on Jupyter (and IPython too, but that's implied). Feel free to squash and merge this in whenever @meghanrjones. |
…d signature (GenericMappingTools#1282) Add a new insert_alias() function in pygmt/helpers/decorators.py that is used by the use_alias() decorator to insert aliases into the method signature, to support tab completion in Jupyter and iPython. Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
Description of proposed changes
This PR adds a decorator that adds aliases as parameters to the signature of PyGMT methods to support tab completion. It is motivated by this suggestion, but with a different implementation. The decorator was added to blockmean and blockmedian as examples. (edit: the decorator was added to all methods that use
use_alias
, but this can easily be reverted if needed). Here is what the result looks like in the docs and a jupyter notebook:I tested that tab completing the alias keywords works in a Jupyter notebook, but not in any other environments.
Addresses #1203
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.Slash Commands
You can write slash commands (
/command
) in the first line of a comment to performspecific operations. Supported slash commands are:
/format
: automatically format and lint the code/test-gmt-dev
: run full tests on the latest GMT development version