8000 MAINT: Improve the description of `AxisError`s overloaded constructor · numpy/numpy@2266599 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2266599

Browse files
< 10000 div class="prc-PageHeader-Description-kFg8r">
committed
MAINT: Improve the description of AxisErrors overloaded constructor
1 parent 6aa5881 commit 2266599

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

numpy/__init__.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3653,11 +3653,12 @@ class RankWarning(UserWarning): ...
36533653
class TooHardError(RuntimeError): ...
36543654

36553655
class AxisError(ValueError, IndexError):
3656-
axis: int
3656+
axis: None | int
36573657
ndim: None | int
3658-
def __init__(
3659-
self, axis: int, ndim: None | int = ..., msg_prefix: None | str = ...
3660-
) -> None: ...
3658+
@overload
3659+
def __init__(self, axis: str, ndim: None = ..., msg_prefix: None = ...) -> None: ...
3660+
@overload
3661+
def __init__(self, axis: int, ndim: int, msg_prefix: None | str = ...) -> None: ...
36613662

36623663
_CallType = TypeVar("_CallType", bound=Union[_ErrFunc, _SupportsWrite])
36633664

numpy/core/_exceptions.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,22 @@ class AxisError(ValueError, IndexError):
133133
134134
Parameters
135135
----------
136-
axis : int
137-
The out of bounds axis.
136+
axis : int or str
137+
The out of bounds axis or a custom exception message.
138+
If an axis is provided, then `ndim` should be specified as well.
138139
ndim : int, optional
139140
The number of array dimensions.
140141
msg_prefix : str, optional
141142
A prefix for the exception message.
142143
143144
Attributes
144145
----------
145-
axis : int
146-
The out of bounds axis.
146+
axis : int, optional
147+
The out of bounds axis or ``None`` if a custom exception
148+
message was provided.
147149
ndim : int, optional
148-
The number of array dimensions.
149-
Defaults to ``None`` if unspecified.
150+
The number of array dimensions or ``None`` if a custom exception
151+
message was provided.
150152
151153
Examples
152154
--------
@@ -156,24 +158,36 @@ class AxisError(ValueError, IndexError):
156158
...
157159
numpy.AxisError: axis 1 is out of bounds for array of dimension 1
158160
161+
The class constructor generally takes the axis and arrays'
162+
dimensionality as arguments:
163+
164+
>>> np.AxisError(2, 1, prefix='error')
165+
numpy.AxisError('error: axis 2 is out of bounds for array of dimension 1')
166+
167+
Alternativelly, a custom exception message can be passed:
168+
169+
>>> np.AxisError('Custom error message')
170+
numpy.AxisError('Custom error message')
171+
159172
"""
160173

161174
__slots__ = ("axis", "ndim")
162175

163176
def __init__(self, axis, ndim=None, msg_prefix=None):
164-
self.axis = axis
165-
self.ndim = ndim
166-
167177
# single-argument form just delegates to base class
168178
if ndim is None and msg_prefix is None:
169179
msg = axis
180+
self.axis = None
181+
self.ndim = None
170182

171183
# do the string formatting here, to save work in the C code
172184
else:
173185
msg = ("axis {} is out of bounds for array of dimension {}"
174186
.format(axis, ndim))
175187
if msg_prefix is not None:
176188
msg = "{}: {}".format(msg_prefix, msg)
189+
self.axis = axis
190+
self.ndim = ndim
177191

178192
super().__init__(msg)
179193

numpy/core/tests/test__exceptions.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,18 @@ def test_pickling(self):
6262
@pytest.mark.parametrize("args", [
6363
(2, 1, None),
6464
(2, 1, "test_prefix"),
65-
(2, None, None),
66-
(2, None, "test_prefix")
65+
("test message",),
6766
])
6867
class TestAxisError:
6968
def test_attr(self, args):
7069
"""Validate attribute types."""
7170
exc = np.AxisError(*args)
72-
assert exc.axis == args[0]
73-
assert exc.ndim == args[1]
71+
if len(args) == 1:
72+
assert exc.axis is None
73+
assert exc.ndim is None
74+
else:
75+
assert exc.axis == axis
76+
assert exc.ndim == ndim
7477

7578
def test_pickling(self, args):
7679
"""Test that `AxisError` can be pickled."""

0 commit comments

Comments
 (0)
0