From 73abbf0717d0cd9d57e371b5f5fd1565b27f0ce1 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 12 Jul 2021 16:36:53 +0200 Subject: [PATCH 01/15] ENH: Add the `axis` and `ndim` attributes to `np.AxisError` --- numpy/__init__.pyi | 4 +++- numpy/core/_exceptions.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index ca0c79b1719f..e9b024891884 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -3653,8 +3653,10 @@ class RankWarning(UserWarning): ... class TooHardError(RuntimeError): ... class AxisError(ValueError, IndexError): + axis: int + ndim: None | int def __init__( - self, axis: int, ndim: Optional[int] = ..., msg_prefix: Optional[str] = ... + self, axis: int, ndim: None | int = ..., msg_prefix: None | str = ... ) -> None: ... _CallType = TypeVar("_CallType", bound=Union[_ErrFunc, _SupportsWrite]) diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index 77aa2f6e1926..8ffc2d64ddd2 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -123,7 +123,13 @@ class TooHardError(RuntimeError): @set_module('numpy') class AxisError(ValueError, IndexError): """ Axis supplied was invalid. """ + + __slots__ = ("axis", "ndim") + def __init__(self, axis, ndim=None, msg_prefix=None): + self.axis = axis + self.ndim = ndim + # single-argument form just delegates to base class if ndim is None and msg_prefix is None: msg = axis From 65dd17756d1ef88e43705861e0ee405165635f1b Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 12 Jul 2021 16:37:45 +0200 Subject: [PATCH 02/15] MAINT: Let the new `AxisError` attributes survive pickling --- numpy/core/_exceptions.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index 8ffc2d64ddd2..8932aa8a119c 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -143,6 +143,20 @@ def __init__(self, axis, ndim=None, msg_prefix=None): super().__init__(msg) + @classmethod + def _reconstruct(cls, axis, ndim, args): + """Auxiliary instance constructor used by `__reduce__`.""" + self = super().__new__(cls) + self.axis = axis + self.ndim = ndim + self.args = args + return self + + def __reduce__(self): + """Return state information for `pickle`.""" + cls = type(self) + return cls._reconstruct, (self.axis, self.ndim, self.args) + @_display_as_base class _ArrayMemoryError(MemoryError): From 0d3962c117dd55ff57979f0f79987c46eea41f25 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 12 Jul 2021 16:39:21 +0200 Subject: [PATCH 03/15] DOC: Update the `AxisError` documentation --- doc/source/reference/routines.other.rst | 9 ++++++- numpy/core/_exceptions.py | 36 ++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/doc/source/reference/routines.other.rst b/doc/source/reference/routines.other.rst index aefd680bbcd1..339857409157 100644 --- a/doc/source/reference/routines.other.rst +++ b/doc/source/reference/routines.other.rst @@ -55,4 +55,11 @@ Matlab-like Functions :toctree: generated/ who - disp \ No newline at end of file + disp + +Exceptions +---------- +.. autosummary:: + :toctree: generated/ + + AxisError diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index 8932aa8a119c..98c00737efa6 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -122,7 +122,41 @@ class TooHardError(RuntimeError): @set_module('numpy') class AxisError(ValueError, IndexError): - """ Axis supplied was invalid. """ + """Axis supplied was invalid. + + Raised whenever an ``axis`` parameter is specified that is larger than + the number of array dimensions. + + .. versionadded:: 1.22 + + Added the ``axis`` and ``ndim`` attributes. + + Parameters + ---------- + axis : int + The out of bounds axis. + ndim : int, optional + The number of array dimensions. + msg_prefix : str, optional + A prefix for the exception message. + + Attributes + ---------- + axis : int + The out of bounds axis. + ndim : int, optional + The number of array dimensions. + Defaults to ``None`` if unspecified. + + Examples + -------- + >>> array_1d = np.arange(10) + >>> np.cumsum(array_1d, axis=1) + Traceback (most recent call last): + ... + numpy.AxisError: axis 1 is out of bounds for array of dimension 1 + + """ __slots__ = ("axis", "ndim") From 2ef77a30ffd97104feb9e28c2610890b5f077a91 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 12 Jul 2021 17:11:31 +0200 Subject: [PATCH 04/15] TST: Update the `AxisError` tests --- numpy/core/tests/test__exceptions.py | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/numpy/core/tests/test__exceptions.py b/numpy/core/tests/test__exceptions.py index 51c056936fd4..49732204da6c 100644 --- a/numpy/core/tests/test__exceptions.py +++ b/numpy/core/tests/test__exceptions.py @@ -4,6 +4,7 @@ import pickle +import pytest import numpy as np _ArrayMemoryError = np.core._exceptions._ArrayMemoryError @@ -56,3 +57,38 @@ class TestUFuncNoLoopError: def test_pickling(self): """ Test that _UFuncNoLoopError can be pickled """ assert isinstance(pickle.dumps(_UFuncNoLoopError), bytes) + + +@pytest.mark.parametrize("args", [ + (2, 1, None), + (2, 1, "test_prefix"), + (2, None, None), + (2, None, "test_prefix") +]) +class TestAxisError: + def test_attr(self, args): + """Validate attribute types.""" + exc = np.AxisError(*args) + assert exc.axis == args[0] + assert exc.ndim == args[1] + + def test_pickling(self, args): + """Test that `AxisError` can be pickled.""" + exc = np.AxisError(*args) + exc2 = pickle.loads(pickle.dumps(exc)) + + assert type(exc) is type(exc2) + for name in ("axis", "ndim", "args"): + attr1 = getattr(exc, name) + attr2 = getattr(exc2, name) + assert attr1 == attr2, name + + def test_pickling_compat(self, args): + """Test that `AxisError` instances pickled prior to NumPy 1.22 + can still be unpickled. + """ + # Attach a `__reduce__` method equivalent to the one used prior to 1.22 + exc = np.AxisError(*args) + exc.__reduce__ = super(np.AxisError, exc).__reduce__ + + assert pickle.loads(pickle.dumps(exc)) From 6aa588109ed29abe6d1f973e57732fcae7430250 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 12 Jul 2021 19:00:06 +0200 Subject: [PATCH 05/15] DOC: Add a release note --- doc/release/upcoming_changes/19459.new_feature.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 doc/release/upcoming_changes/19459.new_feature.rst diff --git a/doc/release/upcoming_changes/19459.new_feature.rst b/doc/release/upcoming_changes/19459.new_feature.rst new file mode 100644 index 000000000000..aecae670feba --- /dev/null +++ b/doc/release/upcoming_changes/19459.new_feature.rst @@ -0,0 +1,4 @@ +The ``ndim`` and ``axis`` attributes have been added to `numpy.AxisError` +------------------------------------------------------------------------- +The ``ndim`` and ``axis`` parameters are now also stored as attributes +within each `numpy.AxisError` instance. From 2e1d0418dd964ab44ee4ac0bb1d8a6f86eda042d Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 12 Jul 2021 21:25:10 +0200 Subject: [PATCH 06/15] MAINT: Improve the description of `AxisError`s overloaded constructor --- numpy/__init__.pyi | 9 ++++---- numpy/core/_exceptions.py | 32 ++++++++++++++++++++-------- numpy/core/tests/test__exceptions.py | 12 +++++++---- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index e9b024891884..cd413c58f685 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -3653,11 +3653,12 @@ class RankWarning(UserWarning): ... class TooHardError(RuntimeError): ... class AxisError(ValueError, IndexError): - axis: int + axis: None | int ndim: None | int - def __init__( - self, axis: int, ndim: None | int = ..., msg_prefix: None | str = ... - ) -> None: ... + @overload + def __init__(self, axis: str, ndim: None = ..., msg_prefix: None = ...) -> None: ... + @overload + def __init__(self, axis: int, ndim: int, msg_prefix: None | str = ...) -> None: ... _CallType = TypeVar("_CallType", bound=Union[_ErrFunc, _SupportsWrite]) diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index 98c00737efa6..18e69c829b37 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -133,8 +133,9 @@ class AxisError(ValueError, IndexError): Parameters ---------- - axis : int - The out of bounds axis. + axis : int or str + The out of bounds axis or a custom exception message. + If an axis is provided, then `ndim` should be specified as well. ndim : int, optional The number of array dimensions. msg_prefix : str, optional @@ -142,11 +143,12 @@ class AxisError(ValueError, IndexError): Attributes ---------- - axis : int - The out of bounds axis. + axis : int, optional + The out of bounds axis or ``None`` if a custom exception + message was provided. ndim : int, optional - The number of array dimensions. - Defaults to ``None`` if unspecified. + The number of array dimensions or ``None`` if a custom exception + message was provided. Examples -------- @@ -156,17 +158,27 @@ class AxisError(ValueError, IndexError): ... numpy.AxisError: axis 1 is out of bounds for array of dimension 1 + The class constructor generally takes the axis and arrays' + dimensionality as arguments: + + >>> np.AxisError(2, 1, prefix='error') + numpy.AxisError('error: axis 2 is out of bounds for array of dimension 1') + + Alternativelly, a custom exception message can be passed: + + >>> np.AxisError('Custom error message') + numpy.AxisError('Custom error message') + """ __slots__ = ("axis", "ndim") def __init__(self, axis, ndim=None, msg_prefix=None): - self.axis = axis - self.ndim = ndim - # single-argument form just delegates to base class if ndim is None and msg_prefix is None: msg = axis + self.axis = None + self.ndim = None # do the string formatting here, to save work in the C code else: @@ -174,6 +186,8 @@ def __init__(self, axis, ndim=None, msg_prefix=None): .format(axis, ndim)) if msg_prefix is not None: msg = "{}: {}".format(msg_prefix, msg) + self.axis = axis + self.ndim = ndim super().__init__(msg) diff --git a/numpy/core/tests/test__exceptions.py b/numpy/core/tests/test__exceptions.py index 49732204da6c..22ddcb06fdab 100644 --- a/numpy/core/tests/test__exceptions.py +++ b/numpy/core/tests/test__exceptions.py @@ -62,15 +62,19 @@ def test_pickling(self): @pytest.mark.parametrize("args", [ (2, 1, None), (2, 1, "test_prefix"), - (2, None, None), - (2, None, "test_prefix") + ("test message",), ]) class TestAxisError: def test_attr(self, args): """Validate attribute types.""" exc = np.AxisError(*args) - assert exc.axis == args[0] - assert exc.ndim == args[1] + if len(args) == 1: + assert exc.axis is None + assert exc.ndim is None + else: + axis, ndim, *_ = args + assert exc.axis == axis + assert exc.ndim == ndim def test_pickling(self, args): """Test that `AxisError` can be pickled.""" From 3f2fe388d3da4435d354fb4c48fd919b827a8194 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 12 Jul 2021 23:02:39 +0200 Subject: [PATCH 07/15] TST: Fix a few typing test failures --- numpy/typing/tests/data/fail/warnings_and_errors.py | 8 +++----- numpy/typing/tests/data/pass/warnings_and_errors.py | 3 +-- numpy/typing/tests/data/reveal/warnings_and_errors.py | 3 ++- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/numpy/typing/tests/data/fail/warnings_and_errors.py b/numpy/typing/tests/data/fail/warnings_and_errors.py index 7390cc45f201..f4fa38293738 100644 --- a/numpy/typing/tests/data/fail/warnings_and_errors.py +++ b/numpy/typing/tests/data/fail/warnings_and_errors.py @@ -1,7 +1,5 @@ import numpy as np -np.AxisError(1.0) # E: Argument 1 to "AxisError" has incompatible type -np.AxisError(1, ndim=2.0) # E: Argument "ndim" to "AxisError" has incompatible type -np.AxisError( - 2, msg_prefix=404 # E: Argument "msg_prefix" to "AxisError" has incompatible type -) +np.AxisError(1.0) # E: No overload variant +np.AxisError(1, ndim=2.0) # E: No overload variant +np.AxisError(2, msg_prefix=404) # E: No overload variant diff --git a/numpy/typing/tests/data/pass/warnings_and_errors.py b/numpy/typing/tests/data/pass/warnings_and_errors.py index 5b6ec2626c0c..a556bf6bcb35 100644 --- a/numpy/typing/tests/data/pass/warnings_and_errors.py +++ b/numpy/typing/tests/data/pass/warnings_and_errors.py @@ -1,7 +1,6 @@ import numpy as np -np.AxisError(1) +np.AxisError("test") np.AxisError(1, ndim=2) -np.AxisError(1, ndim=None) np.AxisError(1, ndim=2, msg_prefix="error") np.AxisError(1, ndim=2, msg_prefix=None) diff --git a/numpy/typing/tests/data/reveal/warnings_and_errors.py b/numpy/typing/tests/data/reveal/warnings_and_errors.py index c428deb7a164..3f20a01350a5 100644 --- a/numpy/typing/tests/data/reveal/warnings_and_errors.py +++ b/numpy/typing/tests/data/reveal/warnings_and_errors.py @@ -7,4 +7,5 @@ reveal_type(np.ComplexWarning()) # E: numpy.ComplexWarning reveal_type(np.RankWarning()) # E: numpy.RankWarning reveal_type(np.TooHardError()) # E: numpy.TooHardError -reveal_type(np.AxisError(1)) # E: numpy.AxisError +reveal_type(np.AxisError("test")) # E: numpy.AxisError +reveal_type(np.AxisError(5, 1)) # E: numpy.AxisError From 38b0fc25b4bc054d399ecfe54e30596888cda21e Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 12 Jul 2021 23:28:02 +0200 Subject: [PATCH 08/15] DOC: Clarify a fix a few parts of the `AxisError` docstrings Co-Authored-By: Eric Wieser <425260+eric-wieser@users.noreply.github.com> --- numpy/core/_exceptions.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index 18e69c829b37..4c0ec2d6ee16 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -145,7 +145,8 @@ class AxisError(ValueError, IndexError): ---------- axis : int, optional The out of bounds axis or ``None`` if a custom exception - message was provided. + message was provided. This should be the axis as passed by + the user, before any normalization to resolve negative indices. ndim : int, optional The number of array dimensions or ``None`` if a custom exception message was provided. @@ -158,13 +159,20 @@ class AxisError(ValueError, IndexError): ... numpy.AxisError: axis 1 is out of bounds for array of dimension 1 + Negative axes are preserved: + + >>> np.cumsum(array_1d, axis=-2) + Traceback (most recent call last): + ... + numpy.AxisError: axis -2 is out of bounds for array of dimension 1 + The class constructor generally takes the axis and arrays' dimensionality as arguments: >>> np.AxisError(2, 1, prefix='error') numpy.AxisError('error: axis 2 is out of bounds for array of dimension 1') - Alternativelly, a custom exception message can be passed: + Alternatively, a custom exception message can be passed: >>> np.AxisError('Custom error message') numpy.AxisError('Custom error message') @@ -193,7 +201,15 @@ def __init__(self, axis, ndim=None, msg_prefix=None): @classmethod def _reconstruct(cls, axis, ndim, args): - """Auxiliary instance constructor used by `__reduce__`.""" + """Auxiliary instance constructor used by `__reduce__`. + + Directly assign all instance attributes without further sanitization. + + This method avoids the `__init__` constructor in order to: + * Circumvent dealing with its axis-based overload. + * Avoid extracting the message prefix from ``self.args`` if applicable. + + """ self = super().__new__(cls) self.axis = axis self.ndim = ndim From cd7fcb34e5cb142e59f4a733db9b642c76be7720 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Tue, 13 Jul 2021 09:38:39 +0200 Subject: [PATCH 09/15] DOC: Reguide fix --- numpy/core/_exceptions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index 4c0ec2d6ee16..ee60de50694b 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -169,13 +169,13 @@ class AxisError(ValueError, IndexError): The class constructor generally takes the axis and arrays' dimensionality as arguments: - >>> np.AxisError(2, 1, prefix='error') - numpy.AxisError('error: axis 2 is out of bounds for array of dimension 1') + >>> np.AxisError(2, 1, msg_prefix='error') + AxisError('error: axis 2 is out of bounds for array of dimension 1') Alternatively, a custom exception message can be passed: >>> np.AxisError('Custom error message') - numpy.AxisError('Custom error message') + AxisError('Custom error message') """ From 0270f4a6b3210dc4d6fda9e3265eb715c13dc969 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Tue, 13 Jul 2021 11:23:03 +0200 Subject: [PATCH 10/15] DOC: Replace `versionadded` with `versionchanged` Co-authored-by: Eric Wieser --- numpy/core/_exceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index ee60de50694b..9888dd505c4b 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -127,7 +127,7 @@ class AxisError(ValueError, IndexError): Raised whenever an ``axis`` parameter is specified that is larger than the number of array dimensions. - .. versionadded:: 1.22 + .. versionchanged:: 1.22 Added the ``axis`` and ``ndim`` attributes. From 16964aeecf841bddc8eb1a01c918e678fcf4ea0a Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 19 Jul 2021 18:10:35 +0200 Subject: [PATCH 11/15] DOC: Mention that `AxisError` is a `ValueError` and `IndexError` subclass Co-Authored-By: Sebastian Berg --- numpy/core/_exceptions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index 9888dd505c4b..7056d6c84eaa 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -124,7 +124,8 @@ class TooHardError(RuntimeError): class AxisError(ValueError, IndexError): """Axis supplied was invalid. - Raised whenever an ``axis`` parameter is specified that is larger than + A `ValueError` and `IndexError` subclass raised whenever an + ``axis`` parameter is specified that is larger than the number of array dimensions. .. versionchanged:: 1.22 From 11f7edfd0eeba13f0affedf54735f889e4c23a24 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 19 Jul 2021 19:34:17 +0200 Subject: [PATCH 12/15] ENH: Delay assembling of the error message until `__str__` is called Co-Authored-By: Eric Wieser <425260+eric-wieser@users.noreply.github.com> --- numpy/core/_exceptions.py | 47 +++++++++------------------- numpy/core/tests/test__exceptions.py | 10 ------ 2 files changed, 15 insertions(+), 42 deletions(-) diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index 7056d6c84eaa..157d78412a8d 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -180,47 +180,30 @@ class AxisError(ValueError, IndexError): """ - __slots__ = ("axis", "ndim") + __slots__ = ("axis", "ndim", "_msg") def __init__(self, axis, ndim=None, msg_prefix=None): - # single-argument form just delegates to base class - if ndim is None and msg_prefix is None: - msg = axis + if ndim is msg_prefix is None: + # single-argument form: directly set the error message + self._msg = axis self.axis = None self.ndim = None - - # do the string formatting here, to save work in the C code else: - msg = ("axis {} is out of bounds for array of dimension {}" - .format(axis, ndim)) - if msg_prefix is not None: - msg = "{}: {}".format(msg_prefix, msg) + self._msg = msg_prefix self.axis = axis self.ndim = ndim - super().__init__(msg) - - @classmethod - def _reconstruct(cls, axis, ndim, args): - """Auxiliary instance constructor used by `__reduce__`. - - Directly assign all instance attributes without further sanitization. - - This method avoids the `__init__` constructor in order to: - * Circumvent dealing with its axis-based overload. - * Avoid extracting the message prefix from ``self.args`` if applicable. - - """ - self = super().__new__(cls) - self.axis = axis - self.ndim = ndim - self.args = args - return self + def __str__(self): + axis = self.axis + ndim = self.ndim - def __reduce__(self): - """Return state information for `pickle`.""" - cls = type(self) - return cls._reconstruct, (self.axis, self.ndim, self.args) + if axis is ndim is None: + return self._msg + else: + msg = f"axis {axis} is out of bounds for array of dimension {ndim}" + if self._msg is not None: + msg = f"{self._msg}: {msg}" + return msg @_display_as_base diff --git a/numpy/core/tests/test__exceptions.py b/numpy/core/tests/test__exceptions.py index 22ddcb06fdab..c87412aa4349 100644 --- a/numpy/core/tests/test__exceptions.py +++ b/numpy/core/tests/test__exceptions.py @@ -86,13 +86,3 @@ def test_pickling(self, args): attr1 = getattr(exc, name) attr2 = getattr(exc2, name) assert attr1 == attr2, name - - def test_pickling_compat(self, args): - """Test that `AxisError` instances pickled prior to NumPy 1.22 - can still be unpickled. - """ - # Attach a `__reduce__` method equivalent to the one used prior to 1.22 - exc = np.AxisError(*args) - exc.__reduce__ = super(np.AxisError, exc).__reduce__ - - assert pickle.loads(pickle.dumps(exc)) From 76968efaa1a1c66556cdea83c6cefa43291eeae3 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 19 Jul 2021 22:40:32 +0200 Subject: [PATCH 13/15] DOC: Update the `AxisError` string representation in its docstring --- numpy/core/_exceptions.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index 157d78412a8d..ddf016e03a18 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -170,13 +170,13 @@ class AxisError(ValueError, IndexError): The class constructor generally takes the axis and arrays' dimensionality as arguments: - >>> np.AxisError(2, 1, msg_prefix='error') - AxisError('error: axis 2 is out of bounds for array of dimension 1') + >>> print(np.AxisError(2, 1, msg_prefix='error')) + error: axis 2 is out of bounds for array of dimension 1 Alternatively, a custom exception message can be passed: - >>> np.AxisError('Custom error message') - AxisError('Custom error message') + >>> print(np.AxisError('Custom error message')) + Custom error message """ From 7613ef481c437b45b315a464f85b4e52e42635d8 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Tue, 20 Jul 2021 11:03:22 +0200 Subject: [PATCH 14/15] DOC: Update `versionadded`-related information Co-Authored-By: Eric Wieser <425260+eric-wieser@users.noreply.github.com> --- numpy/core/_exceptions.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index ddf016e03a18..de54fe5bbaa4 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -124,13 +124,14 @@ class TooHardError(RuntimeError): class AxisError(ValueError, IndexError): """Axis supplied was invalid. - A `ValueError` and `IndexError` subclass raised whenever an - ``axis`` parameter is specified that is larger than - the number of array dimensions. + This is raised whenever an ``axis`` parameter is specified that is larger + than the number of array dimensions. + For compatibility with code written against older numpy versions, which + raised a mixture of `ValueError` and `IndexError` for this situation, this + exception subclasses both to ensure that ``except ValueError`` and + ``except IndexError`` continue to catch `AxisError`s. - .. versionchanged:: 1.22 - - Added the ``axis`` and ``ndim`` attributes. + .. versionadded:: 1.13 Parameters ---------- @@ -148,10 +149,15 @@ class AxisError(ValueError, IndexError): The out of bounds axis or ``None`` if a custom exception message was provided. This should be the axis as passed by the user, before any normalization to resolve negative indices. + + .. versionadded:: 1.22 ndim : int, optional The number of array dimensions or ``None`` if a custom exception message was provided. + .. versionadded:: 1.22 + + Examples -------- >>> array_1d = np.arange(10) From 1e27e0ccd839f3f60832cf7f07f3dfce0fbc3af5 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Tue, 20 Jul 2021 12:35:57 +0200 Subject: [PATCH 15/15] DOC: Fix sphinx warnings --- numpy/core/_exceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index de54fe5bbaa4..3cd8042ce180 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -129,7 +129,7 @@ class AxisError(ValueError, IndexError): For compatibility with code written against older numpy versions, which raised a mixture of `ValueError` and `IndexError` for this situation, this exception subclasses both to ensure that ``except ValueError`` and - ``except IndexError`` continue to catch `AxisError`s. + ``except IndexError`` statements continue to catch `AxisError`. .. versionadded:: 1.13