You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Let PyGMT functions support tab autocompletion #1203
Jupyter and most modern text editors support tab autocompletion, which is useful for typing long function and parameter names. However, currently, most PyGMT functions don't support tab autocompletion, since we use the alias system (use_alias), *args, and **kwargs. The only way I know to enable tab autocompletion is using normal Python parameters instead of relying on the **kwargs and use_alias decorator (see #262 for the original issue).
The solution seems straightforward. For example, for the pygmt.info() function, the current function is defined as:
Since all parameters are defined as normal Python parameters, tab autocompletion should work. The new function definition still keeps the **kwargs parameter, so that single-letter options are still supported (but we are likely to disallow single-letter options in future releases, see #262). Thus, this would be backward compatible, and is the first step to address #262.
The screenshots below show how the autocompletion work after applying the changes in PR #1202:
Nice idea, but just wondering if there's a way to modify the @use_alias decorator to insert the keyword arguments directly (instead of duplicating work for each PyGMT module)?
but just wondering if there's a way to modify the @use_alias decorator to insert the keyword arguments directly (instead of duplicating work for each PyGMT module)?
Here is a quick script to list the parameters:
import pygmt
from inspect import getmembers, isfunction
for module in (pygmt, pygmt.Figure):
for funcname in getmembers(module, isfunction):
func = getattr(module, funcname[0])
if hasattr(func, "aliases"):
print(f"{funcname[0]}:")
for arg, alias in func.aliases.items():
print(f" {alias}=None,")
print("")
The output looks like this. We still need to manually copy the output parameter list into each function.
All functions are manually updated in #1202, so they should all support auto-completion now. The only exception is pygmt.config(), which needs more work.
Actually, I'm trying to fully get rid of the use_alias decorator.
Yes, I understand that we're working towards resolving #262. There are a lot of line changes made in 998a123, much of which appears to be similar/duplicating that of the @use_alias decorator. And this is why I suggested #1203 (comment) to provide a central location tying the aliases/keyword-arguments to the function as an alternative interim solution (instead of introducing such a large diff).
Perhaps we need more discussion and ideas on the best way to proceed on this long-term issue. My argument is that this would be something for v0.5.0, and until we have an automated solution to sync upstream GMT module parameters and documentation (c.f. #895) with PyGMT, there is no way I feel comfortable with disabling single-character parameters in #262 because it would be limiting the utility of PyGMT (albeit one that is undocumented).
we have an automated solution to sync upstream GMT module parameters and documentation (c.f. #895) with PyGMT.
It seems an impossible task to me.
there is no way I feel comfortable with disabling single-character parameters in #262 because it would be limiting the utility of PyGMT (albeit one that is undocumented).
I agree. We probably won't be able to disable single-letter parameters until v1.0.0.
there is no way I feel comfortable with disabling single-character parameters in #262 because it would be limiting the utility of PyGMT (albeit one that is undocumented).
I agree. We probably won't be able to disable single-letter parameters until v1.0.0.
What is the push to disable single letter parameters, even at v1.0.0? I'm not a fan of single-letter parameters and think we should maximize our use of PyGMT parameter names, but does it cost us anything to remove that capability?
@GenericMappingTools/pygmt-maintainers Should I add the available parameters to the function itself (instead of just the @use_alias decorator) in the #1249 (histogram) in line with the proposed changes that @seisman made in #1202?
there is no way I feel comfortable with disabling single-character parameters in #262 because it would be limiting the utility of PyGMT (albeit one that is undocumented).
I agree. We probably won't be able to disable single-letter parameters until v1.0.0.
What is the push to disable single letter parameters, even at v1.0.0? I'm not a fan of single-letter parameters and think we should maximize our use of PyGMT parameter names, but does it cost us anything to remove that capability?
Good question. I am also curious about this. The main limitation that I see to disallowing the use of single-letter parameters is that it would prevent people from using 'cutting-edge' GMT functionality. Each GMT release tends to offer several new single-character options (see https://docs.generic-mapping-tools.org/dev/changes.html), so removing PyGMT support for single-character options would make it so that users would need to wait for a PyGMT release with new aliases for the options. The impact of this is limited by PyGMT's relatively quick release cycle compared to GMT's and the (assumption here) minority of users that build GMT from source or upgrade to the latest GMT version right away, but I think it is still worth considering.
The main limitation that I see to disallowing the use of single-letter parameters is that it would prevent people from using 'cutting-edge' GMT functionality.
I agree. Actually what I meant is to disallow single-letter parameters if they already have aliases. For example, currently, the blockmean function doesn't have an alias for -A, so users can still pass A="a" when calling blockmean. After we add an alias (e.g., field), users can either use A="a" or field="a". I think we should disallow A="a" syntax and raise a warning so that users know that it's recommended to change A="a" to field="a".
Activity
weiji14 commentedon Apr 11, 2021
Nice idea, but just wondering if there's a way to modify the
@use_aliasdecorator to insert the keyword arguments directly (instead of duplicating work for each PyGMT module)?seisman commentedon Apr 11, 2021
Here is a quick script to list the parameters:
The output looks like this. We still need to manually copy the output parameter list into each function.
[-]Let PyGMT functions support tab completion[/-][+]Let PyGMT functions support tab autocompletion[/+]seisman commentedon Apr 11, 2021
All functions are manually updated in #1202, so they should all support auto-completion now. The only exception is
pygmt.config(), which needs more work.weiji14 commentedon Apr 11, 2021
Thanks, but what I meant was to recode the
@use_aliasdecorator to inject the keyword parameters into the functions directly. See e.g. https://code.activestate.com/recipes/577382-keyword-argument-injection-with-python-decorators/. I haven't tested it, but I think it involves adding the keyword arguments to the__dict__attribute of the function.seisman commentedon Apr 12, 2021
Actually, I'm trying to fully get rid of the
use_aliasdecorator.weiji14 commentedon Apr 12, 2021
Yes, I understand that we're working towards resolving #262. There are a lot of line changes made in 998a123, much of which appears to be similar/duplicating that of the
@use_aliasdecorator. And this is why I suggested #1203 (comment) to provide a central location tying the aliases/keyword-arguments to the function as an alternative interim solution (instead of introducing such a large diff).Perhaps we need more discussion and ideas on the best way to proceed on this long-term issue. My argument is that this would be something for v0.5.0, and until we have an automated solution to sync upstream GMT module parameters and documentation (c.f. #895) with PyGMT, there is no way I feel comfortable with disabling single-character parameters in #262 because it would be limiting the utility of PyGMT (albeit one that is undocumented).
seisman commentedon Apr 12, 2021
It seems an impossible task to me.
I agree. We probably won't be able to disable single-letter parameters until v1.0.0.
willschlitzer commentedon Apr 20, 2021
What is the push to disable single letter parameters, even at v1.0.0? I'm not a fan of single-letter parameters and think we should maximize our use of PyGMT parameter names, but does it cost us anything to remove that capability?
willschlitzer commentedon Apr 29, 2021
@GenericMappingTools/pygmt-maintainers Should I add the available parameters to the function itself (instead of just the
@use_aliasdecorator) in the #1249 (histogram) in line with the proposed changes that @seisman made in #1202?maxrjones commentedon May 20, 2021
Good question. I am also curious about this. The main limitation that I see to disallowing the use of single-letter parameters is that it would prevent people from using 'cutting-edge' GMT functionality. Each GMT release tends to offer several new single-character options (see https://docs.generic-mapping-tools.org/dev/changes.html), so removing PyGMT support for single-character options would make it so that users would need to wait for a PyGMT release with new aliases for the options. The impact of this is limited by PyGMT's relatively quick release cycle compared to GMT's and the (assumption here) minority of users that build GMT from source or upgrade to the latest GMT version right away, but I think it is still worth considering.
seisman commentedon May 20, 2021
I agree. Actually what I meant is to disallow single-letter parameters if they already have aliases. For example, currently, the
blockmeanfunction doesn't have an alias for -A, so users can still passA="a"when callingblockmean. After we add an alias (e.g.,field), users can either useA="a"orfield="a". I think we should disallowA="a"syntax and raise a warning so that users know that it's recommended to changeA="a"tofield="a".