Skip to content
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

Prevent 'color' argument to eventplot from overriding 'colors' kwarg (fixes #4297) #4298

Merged
merged 2 commits into from Apr 15, 2015

Conversation

alimuldal
Copy link
Contributor

Fixes issue #4297

@tacaswell tacaswell added this to the next point release milestone Mar 30, 2015
@tacaswell
Copy link
Member

Seems more-or-less reasonable to me.

I am inclined to raise, or at least warn, if the user passes in conflicting input.

attn @efiring

@efiring
Copy link
Member

efiring commented Mar 30, 2015

It looks like colors/color is not the only potential conflict here; maybe they can all be cleared up in one shot. I'm also wondering whether there might be a simple and more general way to handle this sort of thing, which seems to be a symptom of our loose handling of **kwargs. Maybe that should be considered separately. For the present PR, it might be enough to just loop over the pairs of potentially conflicting kwargs so as to handle all of them in a single simple loop.

_color = kwargs.pop('color', None)
# 'colors' kwarg should have a higher priority than 'color'
if colors is None:
colors = _color
Copy link
Member

Choose a reason for hiding this comment

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

Can you add an

else:
    if _color is not None:
         warnings.warn("'color' kwarg ignored")

or something to that effect?

@tacaswell
Copy link
Member

@efiring I am fine with merging this (modulo the warning comment) now. The more general clean up should be done library wide and is a much bigger task than we want to pin on a new contributor.

@alimuldal
Copy link
Contributor Author

Apologies for not following up on this sooner (I've been very busy this week). Following Eric's suggestion, I've added a function to matplotlib.cbook that strips out problem keys from **kwargs dicts and raises an IgnoredKeywordWarning. The unittest now covers all of the potentially conflicting arguments ('color', 'linewidth' and 'linestyle') and checks that the appropriate warning is raised.

# remove 'singular' keys from **kwargs dict in order to prevent them
# being lazily passed as LineCollection props, where they override the
# effect of the 'plural' versions (e.g. 'color' overrides 'colors')
cbook.strip_problem_kwargs(kwargs, 'color', 'linewidth', 'linestyle')
Copy link
Member

Choose a reason for hiding this comment

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

This just discards the 'bad' kwargs which we can't do without a deprecation cycle. There are users out there that have been using color that this will break. This needs to automate the precedence cascade.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I thought that was your intent, based on your suggestion to add a warning to the effect of warnings.warn("'color' kwarg ignored")

Copy link
Member

Choose a reason for hiding this comment

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

I wanted that warning only if the kwarg was actually ignored.

@alimuldal
Copy link
Contributor Author

@tacaswell I think this now gives the behaviour you are looking for

Enforces the priority of a local variable over a potentially conflicting
argument in a kwargs dict, but falls back on the value of the keyword
argument if the local variable's value is None. Raises an
IgnoredKeywordWarning if the keyword argument is specified but ignored.
Copy link
Member

Choose a reason for hiding this comment

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

Can you please add numpydoc style Paramaters and Returns sections to this docstring?

@tacaswell
Copy link
Member

Yes, thank you.

Just a minor docstring change left (I hope), other than that lgtm to me.

attn @efiring

---------
out: any object
If local_var is None then kwargs[kwarg_name] will be returned,
otherwise local_var will be returned and an IgnoredKeywordWarning
Copy link
Member

Choose a reason for hiding this comment

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

This is not correct. -> "otherwise local_var will be returned. If kwargs[kwarg_name] has been overridden by local_var, an IgnoredKeywordWarning will be raised." Actually, I think the sentence about the warning should be in a separate "Raises:" block, shouldn't it?

@alimuldal
Copy link
Contributor Author

@efiring I've written a more general version that can parse several keyword arguments in descending order of priority. The name, signature and docstring have also been changed according to your suggestions.

@alimuldal alimuldal force-pushed the color_prop_override branch 5 times, most recently from b59bf4a to 980e551 Compare April 14, 2015 09:34
@alimuldal
Copy link
Contributor Author

@tacaswell @efiring Are you happy with the new version?

@efiring
Copy link
Member

efiring commented Apr 15, 2015

Looks good to me.

efiring added a commit that referenced this pull request Apr 15, 2015
Prevent 'color' argument to eventplot from overriding 'colors' kwarg (fixes #4297)
@efiring efiring merged commit 27a648a into matplotlib:master Apr 15, 2015
@tacaswell
Copy link
Member

@alimuldal Thank you for the work and your patience working with us on revisions!

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.

None yet

3 participants