ENH: Add the axis and ndim attributes to np.AxisError#19459
ENH: Add the axis and ndim attributes to np.AxisError#19459seberg merged 15 commits intonumpy:mainfrom
axis and ndim attributes to np.AxisError#19459Conversation
| .. autosummary:: | ||
| :toctree: generated/ | ||
|
|
||
| AxisError |
There was a problem hiding this comment.
A few other candidates the might be worthwhile to add here:
Lines 3646 to 3653 in 89babbd
Co-Authored-By: Eric Wieser <425260+eric-wieser@users.noreply.github.com>
Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
|
Currently I'm in favor of either using the current approach with |
seberg
left a comment
There was a problem hiding this comment.
LGTM, I am happy with the patterns as is. Delaying string construction might be worthwhile, but doesn't seem too important?
The one thing I thought might be nice, is to note that AxisError subclasses both IndexError and ValueError, since that is important for try/except clauses. But also fine to improve later, it isn't like we had docs before...
…lass Co-Authored-By: Sebastian Berg <sebastian@sipsolutions.net>
It seems like an easy (and sensible) change to make though, so done as of 16964ae. |
There was a problem hiding this comment.
To be clear, the alternative I'm suggesting is:
class AxisError(ValueError, IndexError):
def __init__(self, axis, ndim=None, msg_prefix=None):
if ndim is None and msg_prefix is None:
# single-argument form (for backwards compatibility) sets just the message
self._msg = axis
self.axis = None
self.ndim = None
else:
self._msg = msg_prefix
self.axis = axis
self.ndim = ndim
def __str__(self):
if self.axis is not None and self.ndim is not None:
msg = "axis {} is out of bounds for array of dimension {}".format(
self.axis, self.ndim)
if self._msg is not None:
msg = "{}: {}".format(self._msg, msg)
return msg
else:
return self._msgWhich also provides an AxisError(10, 3)-style __repr__ for free
Co-Authored-By: Eric Wieser <425260+eric-wieser@users.noreply.github.com>
eric-wieser
left a comment
There was a problem hiding this comment.
Thanks for the iteration. The new docs are great!
I've suggested a possible longer explanation of why we subclass indexerror and valueerror - feel free to reword it. The versionadded definitely seems like a good thing to mention, but I'm not sure how far up the docstring it belongs.
Co-Authored-By: Eric Wieser <425260+eric-wieser@users.noreply.github.com>
|
Great also following up with the |
This PR adds the new
axisandndimattributes to thenp.AxisErrorclass, an addition inspired by similarchanges introduced to
AttributeErrorin Python 3.10.It also provided an excuse to update the classes' documentation and tests, both of which were previously rather lacking.
Examples