-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
converted assert into exception #3060
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
in |
I like this post about assertions: https://mail.python.org/pipermail/python-list/2013-November/660401.html I wouldn't go hog-wild in converting the asserts into exceptions, but |
Thanks for the link, I'll read it more carefully tomorrow. I agree about not going wild. Anyway these two points at the end
convinced me even more that my PR make sense |
Indeed. To highlight two particular paragraphs that I find illuminating:
On Tue, May 13, 2014 at 5:07 PM, Francesco Montesano <
|
I don't really want to do this for 1.4.0, labeled for 1.4.x |
assert(a.get_figure() is self) | ||
if a.get_figure() is not self: | ||
msg = "The Axes must have been created in the present figure" | ||
raise AxesException(msg) |
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.
I quite rarely construct my own exception types, the built-in exceptions are pretty comprehensive. In this case, a ValueError would be just as valuable IMHO (https://docs.python.org/2/library/exceptions.html#exceptions.ValueError). Thoughts?
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.
fine with me.
I'll try to get back with further commits later this week or next one. |
Before I embark myself into |
ps: before anyone asks or complain. I'm going to run the test suite and adjust it if needed once I'm done with removing the asserts |
I would avoid touching the backends just yet, they are on my list of things to re-factor as soon as 1.4 gets out (see #2624 ). |
I've left alone a few asserts on private functions after checking that no user input is directly passed to them. |
this line looks like a but to me. Shouldn't it be If it's a bug, I'll fix it here Besides: I didn't dare to touch |
Please make a separate PR for that bug. |
Also, I think that entire block of functions are candidates to be removed from the library. I have never looked at them before, but scanning them they look like they are replicating much of the functionality that |
@tacaswell: I'll do a separate PR. |
I'm waiting Travis answer in case I need to fix any problem. And I have a question about testing.
If I
It is correct? |
I've installed matplotlib as
The failing tests are:
Now I run |
With python3 I get three extra errors:
The traceback is always the same (except for the actual attribute name)
|
At a minimum this needs a re-base. @pelson @mdboom @efiring Thoughts? I think I am in favor of switching asserts in public facing function into exceptions so we can provide more meaningful error messages/make sane error handling possible. I think private functions should keep using assert as they can in principle be suppressed for efficiency reasons. |
@tacaswell: I agree with that assert vs. exception policy. @montefra: Sorry this has sat here so long. It would be nice to rebase this so we can merge. |
@tacaswell: Yes, I agree also. |
At the moment I'm travelling, so I don't know when I'll do it. For sure I can rebase, test and commit within a week and a half. Is there any hurry? |
@@ -2436,8 +2463,8 @@ def __init__(self, boxin, boxout, **kwargs): | |||
Create a new :class:`BboxTransform` that linearly transforms | |||
points from *boxin* to *boxout*. | |||
""" | |||
assert boxin.is_bbox | |||
assert boxout.is_bbox | |||
if not boxin.is_bbox or not boxout.is_bbox: |
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.
or
should be and
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.
see above
@montefra I left you a bunch of style-related comment and a few places where I was confused by the logic. Thank you for doing this! |
@tacaswell it seems that we now agree 👍 Should I rebase? |
@@ -482,12 +482,17 @@ def table(ax, | |||
rows = len(cellText) | |||
cols = len(cellText[0]) | |||
for row in cellText: | |||
assert len(row) == cols | |||
if len(row) != cols: | |||
msg = "Each row in 'cellText' must have {} columns" |
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.
This formatting does not work with python 2.6, you have to explicitly put in either index or kwargs.
This is getting really close! Just a few minor issues (one class of 2.6 compatibility one class of style consistency). Thank you so much for working on this! |
Please @ ping me when this is updated. |
@tacaswell: on top of your comments, I went through all my modifications to make the whole thing more consistent. ps: I've found a few more |
Awesome. A PR to clean up the format calls else where would be appreciated. |
MNT : converted assert into exception Convert input-validation assertions to raise Exceptions (mostly ValueError) instead.
assert np.prod(angles.shape) == angles.shape[0] == pts.shape[0] | ||
if angles.ndim!=1 or angles.shape[0] != pts.shape[0]: | ||
msg = "'angles' must be a column vector and have same number of" | ||
msg += " rows as 'pts'" |
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.
whoops! looks like we forgot to add a raise
here. This is fixed in #4458
A few days I ago I was experimenting for a possible answer to this question. While trying
I stumbled into an
AssertionError
without any explanation (I had to get the doc ofadd_axes
).My understanding is that
assert
should be reserved for debugging, not to throw errors if the user give a wrong parameter. So I've converted them to exception.I've also noticed that
assert
is used all over the places. If there are no strong opinions in the next weeks I'll to go through the code and convert them to exceptions.