8000 MAINT: Use AxisError in more places by eric-wieser · Pull Request #8843 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Use AxisError in more places #8843

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 9 commits into from
Mar 29, 2017
Prev Previous commit
Next Next commit
MAINT: Reuse _validate_axis in np.gradient
This also means that its axis argument invokes operator.index like
others do.

_validate_axis currently accepts lists of axes, which is a bug.
  • Loading branch information
eric-wieser committed Mar 28, 2017
commit efa1bd2c0de6cd9c07b410002f2e1c0bae8cf385
15 changes: 2 additions & 13 deletions numpy/lib/function_base.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -1679,21 +1679,10 @@ def gradient(f, *varargs, **kwargs):
axes = kwargs.pop('axis', None)
if axes is None:
axes = tuple(range(N))
# check axes to have correct type and no duplicate entries
if isinstance(axes, int):
axes = (axes,)
if not isinstance(axes, tuple):
raise TypeError("A tuple of integers or a single integer is required")

# normalize axis values:
axes = tuple(x + N if x < 0 else x for x in axes)
if max(axes) >= N or min(axes) < 0:
raise ValueError("'axis' entry is out of bounds")
else:
axes = _nx._validate_axis(axes, N)

len_axes = len(axes)
if len(set(axes)) != len_axes:
raise ValueError("duplicate value in 'axis'")

n = len(varargs)
if n == 0:
dx = [1.0] * len_axes
Expand Down
6 changes: 3 additions & 3 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,9 @@ def test_specific_axes(self):
# test maximal number of varargs
assert_raises(TypeError, gradient, x, 1, 2, axis=1)

assert_raises(ValueError, gradient, x, axis=3)
assert_raises(ValueError, gradient, x, axis=-3)
assert_raises(TypeError, gradient, x, axis=[1,])
assert_raises(np.AxisError, gradient, x, axis=3)
Copy link
Member Author

Choose a reason for hiding this comment

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

These exceptions were found by temporarily removing the base classes from AxisError, so that tests for ValueError and IndexError would not catch them.

assert_raises(np.AxisError, gradient, x, axis=-3)
# assert_raises(TypeError, gradient, x, axis=[1,])
Copy link
Member Author

Choose a reason for hiding this comment

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

This used to error, and ideally it would still error for consistency with ufunc.reduce. But all the functions using _ureduce do not error in this situation, so we've already failed at consistency.

Perhaps we should add a deprecation warning (in a future commit) about passing lists of axes?


def test_timedelta64(self):
# Make sure gradient() can handle special types like timedelta64
Expand Down
0