Skip to content

[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

Closed
wants to merge 1 commit into from

Conversation

Carreau
Copy link
Contributor

@Carreau Carreau commented Jul 19, 2019

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.

--

I'm not sure with who I talked about this at scipy – I believe @rabernat was there (?) and can remind me.

…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.
@jklymak jklymak added the status: needs comment/discussion needs consensus on next step label Jul 19, 2019
@timhoffm
Copy link
Member

This is an interesting approach 👍. Some things to consider:

We have at least two common cases of kwargs:

  1. Accepts the same kwargs as , e.g.
    https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.subplots.html
  2. Accepts some Artists properties, e.g.
    https://matplotlib.org/devdocs/api/_as_gen/matplotlib.axes.Axes.quiver.html

Configurability

In 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 array does not make sense.

We probably need a variant which replaces kwargs exhaustingly and one which keeps kwargs for further paramters.

Effect on documentation

This will also add the parameters to the signature in the documentation:
https://23449-1385122-gh.circle-artifacts.com/0/home/circleci/project/doc/build/html/api/_as_gen/matplotlib.pyplot.subplots.html?highlight=subplots#matplotlib.pyplot.subplots

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.

@matthew-brett
Copy link
Contributor

During a SciPy discussion it was brought up that this was sub-optimal for new user.

Me too!

@Carreau
Copy link
Contributor Author

Carreau commented Jul 19, 2019

We probably need a variant which replaces kwargs exhaustingly and one which keeps kwargs for further paramters.

Yes, I believe an exclude=, or include_only= parameter is relatively easy. I'd still wan to be careful of not making it too complicated or too time consuming.

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.

This will also add the parameters to the signature in the documentation

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.

@tacaswell tacaswell added this to the v3.3.0 milestone Aug 24, 2019
@QuLogic QuLogic modified the milestones: v3.3.0, v3.4.0 May 2, 2020
@QuLogic QuLogic modified the milestones: v3.4.0, v3.5.0 Jan 22, 2021
@jklymak jklymak marked this pull request as draft May 8, 2021 17:38
@QuLogic QuLogic modified the milestones: v3.5.0, v3.6.0 Aug 18, 2021
@timhoffm timhoffm modified the milestones: v3.6.0, unassigned Apr 30, 2022
@story645 story645 modified the milestones: unassigned, needs sorting Oct 6, 2022
@github-actions
Copy link

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.

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Jun 23, 2023
@Carreau
Copy link
Contributor Author

Carreau commented Oct 17, 2023

Closing to clean things up, anyone feel free to takover.

@Carreau Carreau closed this Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: inactive Marked by the “Stale” Github Action status: needs comment/discussion needs consensus on next step status: needs rebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants