Skip to content

MAINT: Add parameter checks to polynomial integration functions. #9930

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
Oct 30, 2017

Conversation

charris
Copy link
Member

@charris charris commented Oct 26, 2017

It was not being checked that the `lbnd` and `scl` parameters were
scalars as required.

Closes #9901.

It was not being checked that the `lbnd` and `scl` parameters were
scalars as required.

Closes numpy#9901.
The `lbnd` and `scl` parameters of the various polynomial integration
functions  are documented to be scalars. Add tests that those parameters
are checked and ValueError raised when it is not so.
@charris charris force-pushed the polynomials-add-parameter-checks branch from d58bdf6 to 050cd17 Compare October 26, 2017 19:26
if np.ndim(lbnd) != 0:
raise ValueError("lbnd must be a scalar.")
if np.ndim(scl) != 0:
raise ValueError("scl must be a scalar.")
Copy link
Member

Choose a reason for hiding this comment

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

Nit: errors above and below have no terminating period, nor do the builtin python error messages.

@@ -203,6 +203,9 @@ def test_legint(self):
assert_raises(ValueError, leg.legint, [0], .5)
assert_raises(ValueError, leg.legint, [0], -1)
assert_raises(ValueError, leg.legint, [0], 1, [0, 0])
assert_raises(ValueError, leg.legint, [0], lbnd=[0])
assert_raises(ValueError, leg.legint, [0], scl=[0])
assert_raises(ValueError, leg.legint, [0], axis=.5)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this raise a TypeError?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm. I was just following the documentation ... It would probably be more correct if all the errors checked here were Type errors except possibly the third and sixth.

@charris charris force-pushed the polynomials-add-parameter-checks branch from 050cd17 to 06e62db Compare October 29, 2017 01:13
@charris
Copy link
Member Author

charris commented Oct 29, 2017

Updated.

@@ -214,6 +214,9 @@ def test_chebint(self):
assert_raises(ValueError, cheb.chebint, [0], .5)
assert_raises(ValueError, cheb.chebint, [0], -1)
assert_raises(ValueError, cheb.chebint, [0], 1, [0, 0])
assert_raises(TypeError, cheb.chebint, [0], lbnd=[0])
assert_raises(TypeError, cheb.chebint, [0], scl=[0])
assert_raises(ValueError, cheb.chebint, [0], axis=.5)
Copy link
Member

Choose a reason for hiding this comment

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

This strikes me as backwards. The first two should be ValueErrors, and the second should be a TypeError.

I don't think there is any precedent in numpy for "wrong number of dimensions" → TypeError, but there absolutely is precedent for isinstance(axis, float)TypeError

Copy link
Member Author

Choose a reason for hiding this comment

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

Floats here are fine as long as they are integer valued, same with cnt. Might not be the way things are done now, but this is very old code. What is done is conversion to int followed by a check for equality against the original value.

If you think the first two should be ValueError, then the original was correct. However, the ndim check is for scalar, so passing a list could be considered a TypeError.

Copy link
Member

Choose a reason for hiding this comment

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

I see your point - the int(x) == x check is consistent with ValueError, even if neither are consistent with the axis argument in the rest of numpy.

So yes, I prefered the original - sorry for not being clearer there.

if np.ndim(lbnd) != 0:
raise TypeError("lbnd must be a scalar.")
if np.ndim(scl) != 0:
raise TypeError("scl must be a scalar.")
if iaxis != axis:
raise ValueError("The axis must be integer")
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 operator.index, not int(axis) == axis

Copy link
Member

@eric-wieser eric-wieser Oct 29, 2017

Choose a reason for hiding this comment

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

In fact, this line and iaxis should just be removed, and the normalize_axis_index below allowed to throw a more useful error.

Copy link
Member

Choose a reason for hiding this comment

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

But that's not part of this PR, so don't worry about it

@charris charris force-pushed the polynomials-add-parameter-checks branch from 06e62db to 050cd17 Compare October 29, 2017 15:30
@charris
Copy link
Member Author

charris commented Oct 29, 2017

OK, reverted to original.

@eric-wieser
Copy link
Member

I'm guessing you'd prefer the terminating periods to stay, and I don't think it's worth me arguing over.

Does this need a release note? If not, LGTM.

@charris charris merged commit 0a1e91e into numpy:master Oct 30, 2017
@charris
Copy link
Member Author

charris commented Oct 30, 2017

Non-scalars should have been causing errors or bad results all along, so either a bug fix or just better error messages. I don't think a release note is needed.

@charris charris deleted the polynomials-add-parameter-checks branch October 30, 2017 17:50
@eric-wieser
Copy link
Member

Fair call, thanks! I'll open an issue about the int(x) == x thing at some point that I raised here.

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.

2 participants