Skip to content

Raise TypeError on unsupported kwargs of spy() #11367

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

Merged
merged 1 commit into from
Sep 17, 2018

Conversation

timhoffm
Copy link
Member

@timhoffm timhoffm commented Jun 2, 2018

PR Summary

spy() accepts and silently ignores the kwargs 'extent', 'interpolation' and 'linestyle'. This PR changes the behavior to throw a TypeError instead.

@ImportanceOfBeingErnest
Copy link
Member

While you're at it: wouldn't it make sense to allow for the extent keyword to actually be passed to imshow instead of raising an error? As an application I could think of image comparisson where the image may be a photograph with units on the axes for which you'd want to check sparsity.

@timhoffm timhoffm force-pushed the axes-spy-ignored-kwags branch 2 times, most recently from e6e8fae to 0a1c674 Compare June 3, 2018 18:53
@timhoffm
Copy link
Member Author

timhoffm commented Jun 3, 2018

Actually, the current hard-coded implementation of extent yields unexpected results when using origin='lower'. The image flips, but the vertical scale does not:

fig, axs = plt.subplots(1, 2)
axs[0].spy(a)
axs[1].spy(a, origin='lower')

grafik

The fix is to not handle extent explicitly but let imshow do the work:

  • This allows to use extent explicitly as @ImportanceOfBeingErnest proposed above
  • Also this fixes the above unexpected behavior.

@timhoffm timhoffm force-pushed the axes-spy-ignored-kwags branch 2 times, most recently from 04a7b8d to 393e6db Compare June 10, 2018 09:00
@jklymak
Copy link
Member

jklymak commented Jun 12, 2018

Why do we want to explicitly check for these kwargs? We silently ignore all other superfluous kwargs...

@ImportanceOfBeingErnest
Copy link
Member

No I guess the idea is to pass all arguments to the underlying plot or imshow functions except those that would collide with the presets. For those an error is raised before the underlying function would raise a less understandable error.

I think one could as well pop them from the kwargs and issue a warning instead.

@jklymak
Copy link
Member

jklymak commented Jun 13, 2018

I’d be fine w a warning. But I’m not aware that we blacklist unused kwargs unless they conflict with another kwarg so I’d argue that should be changed.

@timhoffm
Copy link
Member Author

Is this warn-on-unsupported **kwargs, but do not raise a TypeError, an agreed procedure? I have the feeling it's just something that nobody cared to check **kwargs so far.

IMO passing an invalid **kwargs is a fundamental programming error that should be detected and the source code adapted. Warnings may be overlooked. I'd opt for TypeError

If you pass an invalid kwarg to a function without **kwargs, this raises a TypeError. I think would be consistent to do the check so that **kwargs is not a wild-card, but just an abbreviation for for a defined set of parameter names.

@ImportanceOfBeingErnest
Copy link
Member

I think this also depens on what is valid according to the docs.
If the docstring says "further arguments are passed to imshow", a warning would be a good choice, saying "interpolation is ignored and set to 'nearest'".
If the docstring says "you may supply all supported arguments to imshow, except 'interpolation'" raising an error would be justified.

@timhoffm timhoffm force-pushed the axes-spy-ignored-kwags branch from 393e6db to d884b2d Compare June 21, 2018 19:57
@timhoffm
Copy link
Member Author

Changed to just show a warning.

if 'linestyle' in kwargs:
kwargs.pop('linestyle')
warnings.warn(
'"interpolation" keyword argument will be ignored',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be "linestyle" instead of "interpolation"

@timhoffm timhoffm force-pushed the axes-spy-ignored-kwags branch from d884b2d to 736b1d2 Compare June 25, 2018 22:23
@timhoffm
Copy link
Member Author

@dstansby I've fixed your requested change.

@Zac-HD
Copy link
Contributor

Zac-HD commented Jun 27, 2018

Is this warn-on-unsupported **kwargs, but do not raise a TypeError, an agreed procedure? I have the feeling it's just something that nobody cared to check **kwargs so far.

IMO passing an invalid **kwargs is a fundamental programming error that should be detected and the source code adapted. Warnings may be overlooked. I'd opt for TypeError

I agree that passing unused **kwargs should be an error - that's the topic of most of the recent work to convert to explicit keyword-only arguments instead of arbitrary kwargs.

So for what it's worth, I'd strongly recommend raising TypeError instead of a warning as was done in #11137, #11145, #11146, and #11271; this is one of the principles for Matplotlib 3.0!

@timhoffm
Copy link
Member Author

@ImportanceOfBeingErnest @jklymak Is it ok if I switch back to error?

@tacaswell
Copy link
Member

Would it be better to do kwargs.setdefault('linestyle', 'None')? That way if the user does pass it in it is respected (over-ruling spy's opinion of what the linestlye should be) and if they do not we get the opinionated default (which is different than the default of the underlying function).

@ImportanceOfBeingErnest
Copy link
Member

Since my opinion was asked here: Yes raising an error should be fine as well; as commented already, the docs should then explicitely list the forbidden arguments.

A default linestyle argument does not make sense in my view. The reason is simply that you never want any linestyle for a spy plot. (In that sense the use of scatter might actually be more useful for spy, but I think that goes too far here.)

@tacaswell tacaswell modified the milestones: v3.0, v3.1 Jul 8, 2018
@tacaswell
Copy link
Member

Moved to 3.1.

@timhoffm
Copy link
Member Author

I agree with @ImportanceOfBeingErnest that a linestyle does not make sense in this context. This is a special purpose function and connecting the individual markers would be misleading at best.

Concerning warning vs. error: Will switch back to an error.

@anntzer
Copy link
Contributor

anntzer commented Sep 14, 2018

Agree with making conflicting kwargs an error as they are otherwise silently ignored.

@anntzer
Copy link
Contributor

anntzer commented Sep 14, 2018

@jklymak you approved the previous iteration where it was just a warning, are you still ok with the current design?

@jklymak
Copy link
Member

jklymak commented Sep 14, 2018

If there are conflicting arguments an error is fine. If there are unused arguments I’m still wary that this sets up the expectation that we will trap unused args elsewhere and we don’t.

@anntzer
Copy link
Contributor

anntzer commented Sep 14, 2018

Well, trapping unused arguments throughout the library is a bit a work in progress, isn't it?

@jklymak
Copy link
Member

jklymak commented Sep 14, 2018

I dunno, is that something we are trying to do? I'm not against it per-se if folks are willing to do it, it just seems like a heavy lift...

@timhoffm
Copy link
Member Author

In my opinion, we should catch unused arguments. People think they achieve some effect when using these arguments. And we silently ignore their intent. „You can pass xxx but it does nothing.“ is usually not a good API.

Currently not doing this in all the places is no reason for not at least trying to move into the direction. After all, we currently have a mixed state.

@anntzer
Copy link
Contributor

anntzer commented Sep 15, 2018

#7728 for similar "warn-on-unused-kwarg" work.

@anntzer anntzer merged commit 989d061 into matplotlib:master Sep 17, 2018
@anntzer
Copy link
Contributor

anntzer commented Sep 17, 2018

thanks

@timhoffm timhoffm deleted the axes-spy-ignored-kwags branch September 17, 2018 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants