Skip to content

Commit

Permalink
Merge pull request #11681 from dstansby/table-errs
Browse files Browse the repository at this point in the history
Raise exceptions from other exceptions in `table`
  • Loading branch information
mhvk committed Aug 20, 2021
2 parents b02e7b1 + 79fb275 commit ed434a3
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions astropy/table/column.py
Expand Up @@ -608,8 +608,8 @@ def format(self, format_string):
self._format = prev_format
raise ValueError(
"Invalid format for column '{}': could not display "
"values in this column using this format ({})".format(
self.name, err.args[0]))
"values in this column using this format".format(
self.name)) from err

@property
def descr(self):
Expand Down
8 changes: 4 additions & 4 deletions astropy/table/groups.py
Expand Up @@ -191,9 +191,9 @@ def __getitem__(self, item):
indices0, indices1 = self.indices[:-1], self.indices[1:]
try:
i0s, i1s = indices0[item], indices1[item]
except Exception:
except Exception as err:
raise TypeError('Index item for groups attribute must be a slice, '
'numpy mask or int array')
'numpy mask or int array') from err
mask = np.zeros(len(parent), dtype=bool)
# Is there a way to vectorize this in numpy?
for i0, i1 in zip(i0s, i1s):
Expand Down Expand Up @@ -256,10 +256,10 @@ def aggregate(self, func):
vals = func.reduceat(par_col, i0s)
else:
vals = np.array([func(par_col[i0: i1]) for i0, i1 in zip(i0s, i1s)])
except Exception:
except Exception as err:
raise TypeError("Cannot aggregate column '{}' with type '{}'"
.format(par_col.info.name,
par_col.info.dtype))
par_col.info.dtype)) from err

out = par_col.__class__(data=vals,
name=par_col.info.name,
Expand Down
2 changes: 1 addition & 1 deletion astropy/table/meta.py
Expand Up @@ -418,6 +418,6 @@ class TableLoader(AstropyLoader):
try:
header = yaml.load(header_yaml, Loader=TableLoader)
except Exception as err:
raise YamlParseError(str(err))
raise YamlParseError() from err

return header
2 changes: 1 addition & 1 deletion astropy/table/np_utils.py
Expand Up @@ -98,7 +98,7 @@ def get_descrs(arrays, col_name_map):
# Beautify the error message when we are trying to merge columns with incompatible
# types by including the name of the columns that originated the error.
raise TableMergeError("The '{}' columns have incompatible types: {}"
.format(names[0], tme._incompat_types))
.format(names[0], tme._incompat_types)) from tme

# Make sure all input shapes are the same
uniq_shapes = set(col.shape[1:] for col in in_cols)
Expand Down
26 changes: 13 additions & 13 deletions astropy/table/operations.py
Expand Up @@ -64,8 +64,8 @@ def _get_list_of_tables(tables):
else:
try:
tables[ii] = Table([val])
except (ValueError, TypeError):
raise TypeError(f'cannot convert {val} to table column.')
except (ValueError, TypeError) as err:
raise TypeError(f'Cannot convert {val} to table column.') from err

return tables

Expand Down Expand Up @@ -160,8 +160,8 @@ def join_skycoord(distance, distance_func='search_around_sky'):
import astropy.coordinates as coords
try:
distance_func = getattr(coords, distance_func)
except AttributeError:
raise ValueError('distance_func must be a function in astropy.coordinates')
except AttributeError as err:
raise ValueError('distance_func must be a function in astropy.coordinates') from err
else:
from inspect import isfunction
if not isfunction(distance_func):
Expand Down Expand Up @@ -943,7 +943,7 @@ def get_descrs(arrays, col_name_map):
# Beautify the error message when we are trying to merge columns with incompatible
# types by including the name of the columns that originated the error.
raise TableMergeError("The '{}' columns have incompatible types: {}"
.format(names[0], tme._incompat_types))
.format(names[0], tme._incompat_types)) from tme

# Make sure all input shapes are the same
uniq_shapes = set(col.shape[1:] for col in in_cols)
Expand All @@ -968,7 +968,7 @@ def common_dtype(cols):
except metadata.MergeConflictError as err:
tme = TableMergeError(f'Columns have incompatible types {err._incompat_types}')
tme._incompat_types = err._incompat_types
raise tme
raise tme from err


def _get_join_sort_idxs(keys, left, right):
Expand Down Expand Up @@ -1237,11 +1237,11 @@ def _join(left, right, keys=None, join_type='inner',

try:
col[array_mask] = col.info.mask_val
except Exception: # Not clear how different classes will fail here
except Exception as err: # Not clear how different classes will fail here
raise NotImplementedError(
"join requires masking column '{}' but column"
" type {} does not support masking"
.format(out_name, col.__class__.__name__))
.format(out_name, col.__class__.__name__)) from err

# Set the output table column to the new joined column
out[out_name] = col
Expand Down Expand Up @@ -1396,7 +1396,7 @@ def _vstack(arrays, join_type='outer', col_name_map=None, metadata_conflicts='wa
# Beautify the error message when we are trying to merge columns with incompatible
# types by including the name of the columns that originated the error.
raise TableMergeError("The '{}' columns have incompatible types: {}"
.format(out_name, err._incompat_types))
.format(out_name, err._incompat_types)) from err

idx0 = 0
for name, array in zip(in_names, arrays):
Expand All @@ -1414,11 +1414,11 @@ def _vstack(arrays, join_type='outer', col_name_map=None, metadata_conflicts='wa

try:
col[idx0:idx1] = col.info.mask_val
except Exception:
except Exception as err:
raise NotImplementedError(
"vstack requires masking column '{}' but column"
" type {} does not support masking"
.format(out_name, col.__class__.__name__))
.format(out_name, col.__class__.__name__)) from err
idx0 = idx1

out[out_name] = col
Expand Down Expand Up @@ -1517,11 +1517,11 @@ def _hstack(arrays, join_type='outer', uniq_col_name='{col_name}_{table_name}',

try:
col[arr_len:] = col.info.mask_val
except Exception:
except Exception as err:
raise NotImplementedError(
"hstack requires masking column '{}' but column"
" type {} does not support masking"
.format(out_name, col.__class__.__name__))
.format(out_name, col.__class__.__name__)) from err
else:
col = array[name][:n_rows]

Expand Down
2 changes: 1 addition & 1 deletion astropy/table/pprint.py
Expand Up @@ -96,7 +96,7 @@ def _auto_format_func(format_, val):
if val is np.ma.masked:
return str(val)

raise ValueError(f'Format function for value {val} failed: {err}')
raise ValueError(f'Format function for value {val} failed.') from err
# If the user-supplied function handles formatting masked elements, use
# it directly. Otherwise, wrap it in a function that traps them.
try:
Expand Down
2 changes: 1 addition & 1 deletion astropy/table/table.py
Expand Up @@ -3068,7 +3068,7 @@ def _is_mapping(obj):

except Exception as err:
raise ValueError("Unable to insert row because of exception in column '{}':\n{}"
.format(name, err))
.format(name, err)) from err
else:
self._replace_cols(columns)

Expand Down

0 comments on commit ed434a3

Please sign in to comment.