-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Experiement] Auto-expand signature when **kwarg passed to other functions #14857
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
Conversation
…tions. Many functions current takes **kwargs and pass them down. During a SciPy discussion it was brought up that this was sub-optimal for new user. We can use the fact that signature can be faked, and inspect to propagate the actual keyword arguments and substitute them for **kwargs in another. With this, the signature of pyplot.subplots(), which currently as viewed in IPython is plt.subplots( nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw, ) now becomes: plt.subplots( nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, + num=None, + figsize=None, + dpi=None, + facecolor=None, + edgecolor=None, + frameon=True, + FigureClass=<class 'matplotlib.figure.Figure'>, + clear=False, + **kwargs, # used to be **fig_kw, to fix. ) ... which is not totally correct (they are kwargs only...) but you should see the point. This is nice as: 1) it now appear in IPython ? and ?? 2) tab completion pick up these kwargs. And it should have minimal overhead at import time, and is so far limited to function that pass all of **kwarg. Many places in matplotlib are also too dynamic to properly do it, but could allow a manual variant.
This is an interesting approach 👍. Some things to consider: We have at least two common cases of kwargs:
ConfigurabilityIn particular with the artists properties it would be nice to have additional freedom to specify the added parameters. For example in https://matplotlib.org/devdocs/api/_as_gen/matplotlib.axes.Axes.quiver.html the property We probably need a variant which replaces kwargs exhaustingly and one which keeps kwargs for further paramters. Effect on documentationThis will also add the parameters to the signature in the documentation: I don't think that's reasonable as it's getting very verbose and the signature does not match the docstring anymore. While one could consider some additional docstring rewriting for this. I wouldn't go down that road for now. I propose to add a flag so that this can be deactivated for the doc build. |
Me too! |
Yes, I believe an If it's equivalent in time as duplicating all the key word argument in the function signature instead of **kwargs, it may not be worth it.
I figured as much – as for messing with the docstring I'm not a fan of it either. but again this is more a "hey this can be done, and it might be useful"; i'm going to assume matplotlib is not the only project that would need it; and it may be nice to have some of that as an external reusable package. |
Since this Pull Request has not been updated in 60 days, it has been marked "inactive." This does not mean that it will be closed, though it may be moved to a "Draft" state. This helps maintainers prioritize their reviewing efforts. You can pick the PR back up anytime - please ping us if you need a review or guidance to move the PR forward! If you do not plan on continuing the work, please let us know so that we can either find someone to take the PR over, or close it. |
Closing to clean things up, anyone feel free to takover. |
Many functions current takes
**kwargs
and pass them down. During a SciPydiscussion it was brought up that this was sub-optimal for new user.
We can use the fact that signature can be faked, and inspect to
propagate the actual keyword arguments and substitute them for
**kwargs
in another.
With this, the signature of pyplot.subplots(), which currently as viewed
in IPython is
now becomes:
... which is not totally correct (they are kwargs only...) but you
should see the point.
This is nice as:
And it should have minimal overhead at import time, and is so far
limited to function that pass all of **kwarg.
Many places in matplotlib are also too dynamic to properly do it, but
could allow a manual variant.
--
I'm not sure with who I talked about this at scipy – I believe @rabernat was there (?) and can remind me.