8000 ENH: Add the `axis` and `ndim` attributes to `np.AxisError` by BvB93 · Pull Request #19459 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add the axis and ndim attributes to np.AxisError #19459

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 15 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MAINT: Improve the description of AxisErrors overloaded constructor
  • Loading branch information
BvB93 committed Jul 12, 2021
commit 2e1d0418dd964ab44ee4ac0bb1d8a6f86eda042d
9 changes: 5 additions & 4 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
32 changes: 23 additions & 9 deletions numpy/core/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,22 @@ 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
A prefix for the exception message.

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
--------
Expand All @@ -156,24 +158,36 @@ 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:
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.axis = axis
self.ndim = ndim

super().__init__(msg)

Expand Down
12 changes: 8 additions & 4 deletions numpy/core/tests/test__exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
0