Skip to content

Support only positional args in contour. Error if no positional argument. #24749

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 2 commits into from
Jan 27, 2023

Conversation

chahak13
Copy link
Contributor

PR Summary

Closes #24743 (Extended from #24747)

Check if kwargs are passed for [X, Y], Z and if not, check that args has at least one element before accessing it.

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

@chahak13
Copy link
Contributor Author

@oscargus can you take a look at this? I've added some logic to support kwargs too. Let me know if you want to add these changes to the other PR and close this. Thanks!

Copy link
Member

@oscargus oscargus left a comment

Choose a reason for hiding this comment

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

Subject to green etc.

(And possibly the "black-like" formatting... ;-))

(One may consider reordering the logic a bit, but that is only my micro-optimization mindset talking. Like

if "Z" in kwargs:
    if "X" ... and ...
    elif "X" ... ^ ...
    else:
elif 0 < nargs...

but it may be that other people prefer to have it more understandable...)

This is much better than mine!

@oscargus oscargus added API: argument checking topic: contour PR: bugfix Pull requests that fix identified bugs labels Dec 16, 2022
@oscargus oscargus added this to the v3.7.0 milestone Dec 16, 2022
@oscargus oscargus added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Dec 16, 2022
Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

I think the type of the raised error needs to be changed.

Anyone can dismiss this review.

@chahak13
Copy link
Contributor Author

Updated, @tacaswell.

@chahak13 chahak13 force-pushed the 24743_contour_kwargs branch 2 times, most recently from 805e4f0 to cba8e7d Compare December 17, 2022 04:57
Comment on lines 1460 to 1461
"Both or neither 'X' and 'Y' may be passed as kwargs. "
"Passing only one of the two is not allowed."
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"Both or neither 'X' and 'Y' may be passed as kwargs. "
"Passing only one of the two is not allowed."
"Passing only one of 'X' or 'Y' is not allowed."

Copy link
Contributor Author

@chahak13 chahak13 Dec 17, 2022

Choose a reason for hiding this comment

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

Updated.

)
elif (("X" in kwargs) ^ ("Y" in kwargs)) and "Z" in kwargs:
raise TypeError(
"Both or neither 'X' and 'Y' may be passed as kwargs. "
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"Both or neither 'X' and 'Y' may be passed as kwargs. "

You don't need to say both, and this phrase is extremely awkward.

@timhoffm
Copy link
Member

timhoffm commented Dec 17, 2022

Sorry for being late to the party. Are we sure we want to do this? Having positional-only parameters is not the end of the world. Note that we have a lot of similar functions, in which you cannot pass the coordinate parameters by keyword. I guess this holds for almost all functions that use *args for signature overloading. This keyword dance adds complexity. That is still moderate here, but if we go down this route users will expect this to work for all functions. I have serious concerns that we want to fiddle this into already more complex functions like tricontour.

I propose to instead only document that these parameters are keyword-only.

Alternatively, maybe it's worth investigating whether this can be generalized into a decorator to keep the complexity out of the functions and make it applicable throughout the code base. @anntzer you come to my mind when it comes to such types of infrastructure code 😄 .

@anntzer
Copy link
Contributor

anntzer commented Dec 17, 2022

See #11526 (comment) or #22303. I haven't checked whether the situation is different for contour (perhaps not having U, V makes things easier) but at first glance keeping parameters positional-only may be a good idea.

If you want something more infrastruture-y, perhaps something along the lines of #7966 (comment) can be made to work (perhaps with a variant of select_matching_signature that displays all possible signatures in the exception thrown when an invalid call is made).

@chahak13
Copy link
Contributor Author

Based on the discussion, I agree that having it just as positional and erroring out if the required arguments are not found would be better. I can change it to something like my original fix here #24747 (comment)

Does that sound better? I also really like the idea of moving the parsing into a separate function as @anntzer did. I can see merits for first fixing this using just the positional arguments fix and then discussing about making a bigger change on the infra level but I'll defer that decision :)

@tacaswell
Copy link
Member

Based in this discussion I think we should push this to 3.8.

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

Hold for 3.8, anyone can dismiss this once 3.7.x is branched.

@tacaswell tacaswell modified the milestones: v3.7.0, v3.8.0 Dec 18, 2022
@chahak13 chahak13 changed the title Support kwargs calls in contour Support only positional args in contour. Error if no positional argument. Dec 19, 2022
@timhoffm timhoffm requested a review from oscargus December 19, 2022 20:57
@oscargus oscargus removed the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Dec 20, 2022
@chahak13 chahak13 force-pushed the 24743_contour_kwargs branch from d8e791d to 66a8722 Compare December 20, 2022 10:04
@chahak13
Copy link
Contributor Author

I'm not sure why code coverage is failing. I've not added any untested lines. The report is about other files, it seems.

@QuLogic
Copy link
Member

QuLogic commented Dec 22, 2022

Because for some reason no CI reported at all.

@chahak13
Copy link
Contributor Author

Should I change anything then?

@QuLogic
Copy link
Member

QuLogic commented Dec 22, 2022

Probably a rebase would fix it?

@chahak13 chahak13 force-pushed the 24743_contour_kwargs branch from 66a8722 to 756eb1e Compare December 22, 2022 07:14
@chahak13
Copy link
Contributor Author

That seems to have at least triggered all the tests. Let's see. Thanks!

@ksunden
Copy link
Member

ksunden commented Jan 27, 2023

Conflict caused by #24870 which changed the inside of the if/elif structure (and added a separate test, which also conflicts, but is more trivial as both should be kept)

@ksunden ksunden merged commit 6ee6793 into matplotlib:main Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: argument checking PR: bugfix Pull requests that fix identified bugs topic: contour
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: contour raises IndexError if Z is specified as keyword argument
8 participants