-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
ENH: Adding type checks to set_printoptions() #7859
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
@@ -161,6 +165,8 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, | |||
if edgeitems is not None: | |||
_summaryEdgeItems = edgeitems | |||
if precision is not None: | |||
if type(precision) is not int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not isinstance(precision, int):
It might be worth adding type checks for some of the other arguments and value checks (e.g. that |
Yeh. I'll leave it for now, as I think the main issue is passing a dict or some other object as the only argument, and in general we should be promoting duck typing |
Well, there are two somewhat conflicting principles here: Duck typing is a good thing, but failing early rather than late is also a good thing :-). If we can tell at set_printoptions time that we're just going to fail later, then it's better to raise the error immediately. But I also agree with @rkern that this is an improvement. |
Code contents |
Sorry for the misclick... Code comments:
Thanks for contributing! |
No worries! I originally included a comment explaining why the error was being raised, but it was suggested I remove it. I will add it back in if @rkern is happy? Ah ok, I'm new to this - could you possibly run me through the process of adding the test? |
I would argue that duck typing is easy, just do EDIT: of course operator.index is assuming that an ssize_t is sufficient for the precision keyword ;). |
I dislike the comment. The only reason it might be desirable is to explain why there is only type-checking on the one argument. I'd rather you just add basic type-checking to the other arguments instead. |
Well, yes, exactly ;-) For adding a test: tests are just functions whose name starts with |
…correct first arg type
OK. Well it's 1 v 1 on the comment issue, but I decided to add it as I think it could be unclear to someone examining the code a to why there is only one type check. I've added a test in the appropriate place, thanks for the explanation. I've also combined my commits into one to clean it all up. Hopefully this all done and dusted now :) |
If you are going to make a comment, make it a comment and not part of the docs. It's only a puzzle to people reading the source code, not users. But I'd really rather you add basic checks to the other arguments. |
Well, as long as the error makes sense in the other cases, it would not matter, but otherwise I think Robert is right, there is not much gain by not adding the other checks to have a more reasonable error. As to the |
Good points, I will add the I will check all the int parameters that are passed, is there an equally clean way to check the bool or string parameters? I.e. using duck typing. |
Yes, when I said comment I meant a comment next to the check, not anything in the docstring. For type checks: Probably just calling |
I left the boolean argument - it was already parsed by I think now we're implementing duck typing in the right way, raising errors when we need to. |
Just to note, that I realized that |
However, since it is a slight change in behaviour, if we do it, we still might want to note it in the release notes for completeness. |
☔ The latest upstream changes (presumably #8983) made this pull request unmergeable. Please resolve the merge conflicts. |
@tom-bird this seems like a nice addition, feel like rebasing it (to fix merge conflicts) and adding a release note? |
At this point, this PR will probably need to restart from scratch - none of the modified lines exist any more. The idea is still good though, so if we close this PR we should open an issue to replace it. |
@charris I would imagine so, yes. |
See issue #7852.
We raise a TypeError if the first argument isn't of the expected type. Not very Pythonic, but otherwise set_printoptions() doesn't fail, but will cause any subsequent calls to print() to fail with an opaque error. In particular someone could mistakenly pass a dict (rather than an unpacked dict).