def _assert_valid_axis(ndims, axis):
    """Assert the condition `-ndims < axis <= ndims` if `axis` is not `None`."""
    if axis and (axis < -ndims or axis >= ndims):
        raise ValueError('axis = %d not in [%d, %d)' % (axis, -ndims, ndims))
