Skip to content
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

refactor: Added raise from clause to exceptions for improved tracebacks #27918

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions ivy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ def unknown_shape(rank=None, **kwargs):
def with_rank(self, rank):
try:
return self.merge_with(self.unknown_shape(rank=rank))
except ValueError:
raise ValueError(f"Shape {self} must have rank {rank}")
except ValueError as e:
raise ValueError(f"Shape {self} must have rank {rank}") from e

def with_rank_at_least(self, rank):
if self.rank is not None and self.rank < rank:
Expand Down
2 changes: 1 addition & 1 deletion ivy/functional/backends/jax/data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def broadcast_arrays(*arrays: JaxArray) -> List[JaxArray]:
try:
return jnp.broadcast_arrays(*arrays)
except ValueError as e:
raise ivy.utils.exceptions.IvyBroadcastShapeError(e)
raise ivy.utils.exceptions.IvyBroadcastShapeError(e) from e


def broadcast_to(
Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/backends/jax/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,11 @@ def take(
if ivy.exists(axis):
try:
x_shape = x.shape[axis]
except Exception:
except Exception as e:
raise ValueError(
f"axis {axis} is out of bounds for array of dimension"
f" {len(x.shape)}"
)
) from e
else:
x_shape = jnp.prod(x.shape)

Expand Down
6 changes: 3 additions & 3 deletions ivy/functional/backends/jax/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def concat(
try:
return jnp.concatenate(xs, axis)
except ValueError as error:
raise ivy.utils.exceptions.IvyIndexError(error)
raise ivy.utils.exceptions.IvyIndexError(error) from error


def expand_dims(
Expand All @@ -54,7 +54,7 @@ def expand_dims(
ret = jnp.expand_dims(x, axis)
return ret
except ValueError as error:
raise ivy.utils.exceptions.IvyIndexError(error)
raise ivy.utils.exceptions.IvyIndexError(error) from error


def flip(
Expand Down Expand Up @@ -143,7 +143,7 @@ def stack(
try:
return jnp.stack(arrays, axis=axis)
except ValueError as error:
raise ivy.utils.exceptions.IvyIndexError(error)
raise ivy.utils.exceptions.IvyIndexError(error) from error


# Extra #
Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/backends/jax/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def all(
try:
return jnp.all(x, axis, keepdims=keepdims)
except ValueError as error:
raise ivy.utils.exceptions.IvyIndexError(error)
raise ivy.utils.exceptions.IvyIndexError(error) from error


def any(
Expand All @@ -34,4 +34,4 @@ def any(
try:
return jnp.any(x, axis, keepdims=keepdims, out=out)
except ValueError as error:
raise ivy.utils.exceptions.IvyIndexError(error)
raise ivy.utils.exceptions.IvyIndexError(error) from error
2 changes: 1 addition & 1 deletion ivy/functional/backends/numpy/data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def broadcast_arrays(*arrays: np.ndarray) -> List[np.ndarray]:
try:
return np.broadcast_arrays(*arrays)
except ValueError as e:
raise ivy.utils.exceptions.IvyBroadcastShapeError(e)
raise ivy.utils.exceptions.IvyBroadcastShapeError(e) from e


@with_unsupported_dtypes({"1.26.3 and below": ("complex",)}, backend_version)
Expand Down
2 changes: 1 addition & 1 deletion ivy/functional/backends/numpy/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def all(
try:
return np.asarray(np.all(x, axis=axis, keepdims=keepdims, out=out))
except np.AxisError as error:
raise ivy.utils.exceptions.IvyIndexError(error)
raise ivy.utils.exceptions.IvyIndexError(error) from error


all.support_native_out = True
Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/backends/paddle/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def take(
if ivy.exists(axis):
try:
x_shape = x.shape[axis]
except Exception:
except Exception as e:
rank = len(x.shape)
raise IndexError(
"(OutOfRange) Attr(axis) is out of range, "
Expand All @@ -784,7 +784,7 @@ def take(
"(0 - input_dim.size()) == true, "
"but received axis < input_dim.size() && axis >= "
"(0 - input_dim.size()):0 != true:1.]"
)
) from e
else:
x_shape = paddle.prod(paddle.to_tensor(x.shape))

Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/backends/paddle/experimental/norms.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ def batch_norm(
if data_format[-1] == "C"
else data_formats[0:4][x.ndim - 2]
)
except IndexError:
except IndexError as e:
raise IndexError(
"data_format must be one of 'NC', 'NCL', 'NCHW', 'NCDHW', 'NLC', 'NHWC',"
f" 'NDHWC' but receive {data_format}"
)
) from e

with ivy.ArrayMode(False):
if training:
Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/backends/tensorflow/data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ def broadcast_arrays(
try:
desired_shape = tf.broadcast_dynamic_shape(arrays[0].shape, arrays[1].shape)
except tf.errors.InvalidArgumentError as e:
raise ivy.utils.exceptions.IvyBroadcastShapeError(e)
raise ivy.utils.exceptions.IvyBroadcastShapeError(e) from e
if len(arrays) > 2:
for i in range(2, len(arrays)):
try:
desired_shape = tf.broadcast_dynamic_shape(
desired_shape, arrays[i].shape
)
except tf.errors.InvalidArgumentError as e:
raise ivy.utils.exceptions.IvyBroadcastShapeError(e)
raise ivy.utils.exceptions.IvyBroadcastShapeError(e) from e
else:
return [arrays[0]]
result = []
Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/backends/tensorflow/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def expand_dims(
ret = tf.reshape(x, shape=out_shape)
return ret
except (tf.errors.InvalidArgumentError, np.AxisError) as error:
raise ivy.utils.exceptions.IvyIndexError(error)
raise ivy.utils.exceptions.IvyIndexError(error) from error


def flip(
Expand Down Expand Up @@ -196,7 +196,7 @@ def stack(
try:
return tf.experimental.numpy.stack(arrays, axis)
except ValueError as e:
raise ivy.utils.exceptions.IvyIndexError(e)
raise ivy.utils.exceptions.IvyIndexError(e) from e


# Extra #
Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/backends/tensorflow/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def all(
try:
return tf.reduce_all(tf.cast(x, tf.bool), axis=axis, keepdims=keepdims)
except tf.errors.InvalidArgumentError as e:
raise ivy.utils.exceptions.IvyIndexError(e)
raise ivy.utils.exceptions.IvyIndexError(e) from e


def any(
Expand All @@ -44,4 +44,4 @@ def any(
try:
return tf.reduce_any(tf.cast(x, tf.bool), axis=axis, keepdims=keepdims)
except tf.errors.InvalidArgumentError as e:
raise ivy.utils.exceptions.IvyIndexError(e)
raise ivy.utils.exceptions.IvyIndexError(e) from e
2 changes: 1 addition & 1 deletion ivy/functional/backends/torch/data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def broadcast_arrays(*arrays: torch.Tensor) -> List[torch.Tensor]:
try:
return list(torch.broadcast_tensors(*arrays))
except RuntimeError as e:
raise ivy.utils.exceptions.IvyBroadcastShapeError(e)
raise ivy.utils.exceptions.IvyBroadcastShapeError(e) from e


def broadcast_to(
Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/backends/torch/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,13 @@ def take(
if ivy.exists(axis):
try:
x_shape = x.shape[axis]
except Exception:
except Exception as e:
rank = len(x.shape)
raise IndexError(
"IndexError: Dimension out of range"
f"(expected to be in range of[-{rank}, {rank-1}]"
f", but got {axis})"
)
) from e
else:
x_shape = torch.prod(torch.tensor(x.shape))

Expand Down
8 changes: 4 additions & 4 deletions ivy/functional/backends/torch/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,10 @@ def scatter_flat(
if torch_scatter is None:
try:
import torch_scatter as torch_scatter
except ImportError:
except ImportError as e:
raise ivy.utils.exceptions.IvyException(
"Unable to import torch_scatter, verify this is correctly installed."
)
) from e
if reduction == "replace":
output[indices.type(torch.int64)] = updates
res = output
Expand Down Expand Up @@ -478,10 +478,10 @@ def scatter_nd(
if torch_scatter is None:
try:
import torch_scatter as torch_scatter
except ImportError:
except ImportError as e:
raise ivy.utils.exceptions.IvyException(
"Unable to import torch_scatter, verify this is correctly installed."
)
) from e
if reduction == "replace":
flat_output[flat_indices_for_flat] = flat_updates
flat_scatter = flat_output
Expand Down
6 changes: 4 additions & 2 deletions ivy/functional/frontends/jax/numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,10 @@ def promote_types_jax(
"""
try:
ret = jax_promotion_table[(ivy.as_ivy_dtype(type1), ivy.as_ivy_dtype(type2))]
except KeyError:
raise ivy.utils.exceptions.IvyException("these dtypes are not type promotable")
except KeyError as e:
raise ivy.utils.exceptions.IvyException(
"these dtypes are not type promotable"
) from e
return ret


Expand Down
6 changes: 4 additions & 2 deletions ivy/functional/frontends/mxnet/numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ def promote_types_mxnet(
"""
try:
ret = mxnet_promotion_table[(ivy.as_ivy_dtype(type1), ivy.as_ivy_dtype(type2))]
except KeyError:
raise ivy.utils.exceptions.IvyException("these dtypes are not type promotable")
except KeyError as e:
raise ivy.utils.exceptions.IvyException(
"these dtypes are not type promotable"
) from e
return ret


Expand Down
6 changes: 4 additions & 2 deletions ivy/functional/frontends/numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,10 @@ def promote_numpy_dtypes(
type1, type2 = ivy.as_ivy_dtype(type1), ivy.as_ivy_dtype(type2)
try:
return numpy_promotion_table[(type1, type2)]
except KeyError:
raise ivy.utils.exceptions.IvyException("these dtypes are not type promotable")
except KeyError as e:
raise ivy.utils.exceptions.IvyException(
"these dtypes are not type promotable"
) from e


@handle_exceptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def __repr__(self):
def __ge__(self, other):
try:
other = dtype(other)
except TypeError:
except TypeError as e:
raise ivy.utils.exceptions.IvyException(
"Attempted to compare a dtype with something which"
"couldn't be interpreted as a dtype"
)
) from e

return self == np_frontend.promote_numpy_dtypes(
self._ivy_dtype, other._ivy_dtype
Expand All @@ -32,22 +32,22 @@ def __ge__(self, other):
def __gt__(self, other):
try:
other = dtype(other)
except TypeError:
except TypeError as e:
raise ivy.utils.exceptions.IvyException(
"Attempted to compare a dtype with something which"
"couldn't be interpreted as a dtype"
)
) from e

return self >= other and self != other

def __lt__(self, other):
try:
other = dtype(other)
except TypeError:
except TypeError as e:
raise ivy.utils.exceptions.IvyException(
"Attempted to compare a dtype with something which"
"couldn't be interpreted as a dtype"
)
) from e

return self != np_frontend.promote_numpy_dtypes(
self._ivy_dtype, other._ivy_dtype
Expand All @@ -56,11 +56,11 @@ def __lt__(self, other):
def __le__(self, other):
try:
other = dtype(other)
except TypeError:
except TypeError as e:
raise ivy.utils.exceptions.IvyException(
"Attempted to compare a dtype with something which"
"couldn't be interpreted as a dtype"
)
) from e

return self < other or self == other

Expand Down
6 changes: 4 additions & 2 deletions ivy/functional/frontends/onnx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ def promote_types_onnx(
"""
try:
ret = onnx_promotion_table[(ivy.as_ivy_dtype(type1), ivy.as_ivy_dtype(type2))]
except KeyError:
raise ivy.utils.exceptions.IvyException("these dtypes are not type promotable")
except KeyError as e:
raise ivy.utils.exceptions.IvyException(
"these dtypes are not type promotable"
) from e
return ret


Expand Down
6 changes: 4 additions & 2 deletions ivy/functional/frontends/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ def promote_types_paddle(
"""
try:
ret = paddle_promotion_table[(ivy.as_ivy_dtype(type1), ivy.as_ivy_dtype(type2))]
except KeyError:
raise ivy.utils.exceptions.IvyException("these dtypes are not type promotable")
except KeyError as e:
raise ivy.utils.exceptions.IvyException(
"these dtypes are not type promotable"
) from e
return ret


Expand Down
6 changes: 4 additions & 2 deletions ivy/functional/frontends/torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ def promote_types_torch(
ret = torch_frontend.torch_promotion_table[
(ivy.as_ivy_dtype(type1), ivy.as_ivy_dtype(type2))
]
except KeyError:
raise ivy.utils.exceptions.IvyException("these dtypes are not type promotable")
except KeyError as e:
raise ivy.utils.exceptions.IvyException(
"these dtypes are not type promotable"
) from e
return ret


Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/frontends/torch/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def cholesky_ex(input, *, upper=False, check_errors=False, out=None):
return matrix, info
except RuntimeError as e:
if check_errors:
raise RuntimeError(e)
raise RuntimeError(e) from e
else:
matrix = input * math.nan
info = ivy.ones(input.shape[:-2], dtype=ivy.int32)
Expand Down Expand Up @@ -292,7 +292,7 @@ def solve_ex(A, B, *, left=True, check_errors=False, out=None):
return result, info
except RuntimeError as e:
if check_errors:
raise RuntimeError(e)
raise RuntimeError(e) from e
else:
result = A * math.nan
info = ivy.ones(A.shape[:-2], dtype=ivy.int32)
Expand Down
Loading
Loading