Skip to content

Commit

Permalink
Remove error utils function (not used anymore) (zarr-developers#614)
Browse files Browse the repository at this point in the history
* Remove error utils function (not used anymore)

Convert also one more err_ function into a proper exception that derives
from ValueError.

* turn more function into errors

* use VindexInvalidSelectionError
  • Loading branch information
Carreau authored Oct 2, 2020
1 parent 610db34 commit 9fe9a0e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 48 deletions.
4 changes: 2 additions & 2 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Next release
By :user:`Elliott Sales de Andrade <QuLogic>`; :issue:`442`

* Many of the convenience functions to emit errors (``err_*`` from
``zarr.errors`` have been replaced by ``ValueError`` subclasses. The
functions are deprecated and will be removed in the future. :issue:`590` )
``zarr.errors`` have been replaced by ``ValueError`` subclasses. The corresponding
``err_*`` function have been removed. :issue:`590`, :issue:`614`)

* Improve consistency of terminology regarding arrays and datasets in the
documentation.
Expand Down
56 changes: 20 additions & 36 deletions zarr/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,35 @@ def __init__(self, *args):
super().__init__(self._msg.format(*args))


class ContainsGroupError(_BaseZarrError):
_msg = "path {0!r} contains a group"
class _BaseZarrIndexError(IndexError):
_msg = ""

def __init__(self, *args):
super().__init__(self._msg.format(*args))


def err_contains_group(path):
raise ContainsGroupError(path) # pragma: no cover
class ContainsGroupError(_BaseZarrError):
_msg = "path {0!r} contains a group"


class ContainsArrayError(_BaseZarrError):
_msg = "path {0!r} contains an array"


def err_contains_array(path):
raise ContainsArrayError(path) # pragma: no cover


class ArrayNotFoundError(_BaseZarrError):
_msg = "array not found at path %r' {0!r}"


def err_array_not_found(path):
raise ArrayNotFoundError(path) # pragma: no cover


class GroupNotFoundError(_BaseZarrError):
_msg = "group not found at path {0!r}"


def err_group_not_found(path):
raise GroupNotFoundError(path) # pragma: no cover


class PathNotFoundError(_BaseZarrError):
_msg = "nothing found at path {0!r}"


def err_path_not_found(path):
raise PathNotFoundError(path) # pragma: no cover


def err_bad_compressor(compressor):
raise ValueError('bad compressor; expected Codec object, found %r' %
compressor)
class BadCompressorError(_BaseZarrError):
_msg = "bad compressor; expected Codec object, found {0!r}"


class FSPathExistNotDir(GroupNotFoundError):
Expand All @@ -69,25 +55,23 @@ def __init__(self):
super().__init__("object is read-only")


def err_read_only():
raise ReadOnlyError() # pragma: no cover

class BoundsCheckError(_BaseZarrIndexError):
_msg = "index out of bounds for dimension with length {0}"

def err_boundscheck(dim_len):
raise IndexError('index out of bounds for dimension with length {}'
.format(dim_len))


def err_negative_step():
raise IndexError('only slices with step >= 1 are supported')
class NegativeStepError(IndexError):
def __init__(self):
super().__init__("only slices with step >= 1 are supported")


def err_too_many_indices(selection, shape):
raise IndexError('too many indices for array; expected {}, got {}'
.format(len(shape), len(selection)))


def err_vindex_invalid_selection(selection):
raise IndexError('unsupported selection type for vectorized indexing; only '
'coordinate selection (tuple of integer arrays) and mask selection '
'(single Boolean array) are supported; got {!r}'.format(selection))
class VindexInvalidSelectionError(_BaseZarrIndexError):
_msg = (
"unsupported selection type for vectorized indexing; only "
"coordinate selection (tuple of integer arrays) and mask selection "
"(single Boolean array) are supported; got {0!r}"
)
18 changes: 11 additions & 7 deletions zarr/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

import numpy as np

from zarr.errors import (err_boundscheck, err_negative_step,
err_too_many_indices, err_vindex_invalid_selection)
from zarr.errors import (
NegativeStepError,
err_too_many_indices,
VindexInvalidSelectionError,
BoundsCheckError,
)


def is_integer(x):
Expand Down Expand Up @@ -46,7 +50,7 @@ def normalize_integer_selection(dim_sel, dim_len):

# handle out of bounds
if dim_sel >= dim_len or dim_sel < 0:
err_boundscheck(dim_len)
raise BoundsCheckError(dim_len)

return dim_sel

Expand Down Expand Up @@ -101,7 +105,7 @@ def __init__(self, dim_sel, dim_len, dim_chunk_len):
# normalize
self.start, self.stop, self.step = dim_sel.indices(dim_len)
if self.step < 1:
err_negative_step()
raise NegativeStepError()

# store attributes
self.dim_len = dim_len
Expand Down Expand Up @@ -385,7 +389,7 @@ def wraparound_indices(x, dim_len):

def boundscheck_indices(x, dim_len):
if np.any(x < 0) or np.any(x >= dim_len):
err_boundscheck(dim_len)
raise BoundsCheckError(dim_len)


class IntArrayDimIndexer(object):
Expand Down Expand Up @@ -756,7 +760,7 @@ def __getitem__(self, selection):
elif is_mask_selection(selection, self.array):
return self.array.get_mask_selection(selection, fields=fields)
else:
err_vindex_invalid_selection(selection)
raise VindexInvalidSelectionError(selection)

def __setitem__(self, selection, value):
fields, selection = pop_fields(selection)
Expand All @@ -767,7 +771,7 @@ def __setitem__(self, selection, value):
elif is_mask_selection(selection, self.array):
self.array.set_mask_selection(selection, value, fields=fields)
else:
err_vindex_invalid_selection(selection)
raise VindexInvalidSelectionError(selection)


def check_fields(fields, dtype):
Expand Down
6 changes: 3 additions & 3 deletions zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

from zarr.errors import (
MetadataError,
err_bad_compressor,
BadCompressorError,
ContainsArrayError,
ContainsGroupError,
FSPathExistNotDir,
Expand Down Expand Up @@ -397,8 +397,8 @@ def _init_array_metadata(
if compressor:
try:
compressor_config = compressor.get_config()
except AttributeError:
err_bad_compressor(compressor)
except AttributeError as e:
raise BadCompressorError(compressor) from e

# obtain filters config
if filters:
Expand Down

0 comments on commit 9fe9a0e

Please sign in to comment.