Repository: scipy/scipy
File: scipy/sparse/_construct.py (Python)

Code before:

    Parameters
    ----------
    A : sparse array
    axis1 : int
        First axis.
    axis2 : int
        Second axis.

    Returns
    -------
    a_swapped : sparse array in COO format
        A copy of the input array with the two identified axes swapped.

    Raises
    ------
    ValueError
        If provided a non-integer or out of range ``[-N, N-1]`` axis,
        where ``N`` is ``A.ndim``.
    """
    axes = np.arange(A.ndim)
    try:
        axes[[axis1, axis2]] = axes[[axis2, axis1]]
    except IndexError as err:
        # message is like: Invalid axis -4 is out of bounds for axis 0 with size 2
        raise ValueError(f"Invalid axis {str(err)[6:]}")
    axes = tuple(axes)
    return permute_dims(A, axes=axes, copy=True)


def permute_dims(A, axes=None, copy=False):
    """Permute the axes of the sparse array `A` to the order `axes`.

    Parameters
    ----------
    A : sparse array
    axes : tuple or list of ints, optional
        If specified, it must be a tuple or list which contains a permutation
        of ``[0, 1, ..., N-1]`` where ``N`` is the number of axes of `A`. The ``i``th
        axis of the returned array will correspond to the axis numbered ``axes[i]``
        of the input. If not specified, defaults to ``range(a.ndim)[::-1]``,
        which reverses the order of the axes.
    copy : bool, optional (default: False)
        Whether to return the permutation as a copy. If False, an in-place
        permutation is provided if possible depending on format.

    Returns
    -------
    out : sparse array in COO format
        A copy of ``A`` with permuted axes.


Review comment (suggestion):
```suggestion
        # original msg looks like: "index -4 is out of bounds for axis 0 with size 2"
        msg = str(e)
        msg = msg.removeprefix('index ').split(' axis ', 1)[0]
        # Final error is: "Invalid axis: -4 is out of bounds for ndim=2"
        raise ValueError(f"Invalid axis: {msg} ndim={A.ndim}")
```

Code after:

    Parameters
    ----------
    A : sparse array
    axis1 : int
        First axis.
    axis2 : int
        Second axis.

    Returns
    -------
    a_swapped : sparse array in COO format
        A copy of the input array with the two identified axes swapped.

    Raises
    ------
    ValueError
        If provided a non-integer or out of range ``[-N, N-1]`` axis,
        where ``N`` is ``A.ndim``.
    """
    axes = np.arange(A.ndim)
    try:
        axes[[axis1, axis2]] = axes[[axis2, axis1]]
    except IndexError as err:
        # original msg looks like: "index -4 is out of bounds for axis 0 with size 2"
        msg = str(err)
        msg = msg.removeprefix('index ').split(' axis ', 1)[0]
        # Final error is: "Invalid axis: -4 is out of bounds for ndim=2"
        raise ValueError(f"Invalid axis: {msg} ndim={A.ndim}")

    axes = tuple(axes)
    return permute_dims(A, axes=axes, copy=True)


def permute_dims(A, axes=None, copy=False):
    """Permute the axes of the sparse array `A` to the order `axes`.

    Parameters
    ----------
    A : sparse array
    axes : tuple or list of ints, optional
        If specified, it must be a tuple or list which contains a permutation
        of ``[0, 1, ..., N-1]`` where ``N`` is ``A.ndim``. The ith
        axis of the returned array will correspond to the axis numbered ``axes[i]``
        of the input. If not specified, defaults to ``range(A.ndim)[::-1]``,
        which reverses the order of the axes.
    copy : bool, optional (default: False)
        Whether to return the permutation as a copy. If False, an in-place
        permutation is provided if possible depending on format.

    Returns