Skip to content

Commit 9fe9a0e

Browse files
authored
Remove error utils function (not used anymore) (#614)
* 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
1 parent 610db34 commit 9fe9a0e

File tree

4 files changed

+36
-48
lines changed

4 files changed

+36
-48
lines changed

docs/release.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Next release
2424
By :user:`Elliott Sales de Andrade <QuLogic>`; :issue:`442`
2525

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

3030
* Improve consistency of terminology regarding arrays and datasets in the
3131
documentation.

zarr/errors.py

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,35 @@ def __init__(self, *args):
1515
super().__init__(self._msg.format(*args))
1616

1717

18-
class ContainsGroupError(_BaseZarrError):
19-
_msg = "path {0!r} contains a group"
18+
class _BaseZarrIndexError(IndexError):
19+
_msg = ""
20+
21+
def __init__(self, *args):
22+
super().__init__(self._msg.format(*args))
2023

2124

22-
def err_contains_group(path):
23-
raise ContainsGroupError(path) # pragma: no cover
25+
class ContainsGroupError(_BaseZarrError):
26+
_msg = "path {0!r} contains a group"
2427

2528

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

2932

30-
def err_contains_array(path):
31-
raise ContainsArrayError(path) # pragma: no cover
32-
33-
3433
class ArrayNotFoundError(_BaseZarrError):
3534
_msg = "array not found at path %r' {0!r}"
3635

3736

38-
def err_array_not_found(path):
39-
raise ArrayNotFoundError(path) # pragma: no cover
40-
41-
4237
class GroupNotFoundError(_BaseZarrError):
4338
_msg = "group not found at path {0!r}"
4439

4540

46-
def err_group_not_found(path):
47-
raise GroupNotFoundError(path) # pragma: no cover
48-
49-
5041
class PathNotFoundError(_BaseZarrError):
5142
_msg = "nothing found at path {0!r}"
5243

5344

54-
def err_path_not_found(path):
55-
raise PathNotFoundError(path) # pragma: no cover
56-
57-
58-
def err_bad_compressor(compressor):
59-
raise ValueError('bad compressor; expected Codec object, found %r' %
60-
compressor)
45+
class BadCompressorError(_BaseZarrError):
46+
_msg = "bad compressor; expected Codec object, found {0!r}"
6147

6248

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

7157

72-
def err_read_only():
73-
raise ReadOnlyError() # pragma: no cover
74-
58+
class BoundsCheckError(_BaseZarrIndexError):
59+
_msg = "index out of bounds for dimension with length {0}"
7560

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

80-
81-
def err_negative_step():
82-
raise IndexError('only slices with step >= 1 are supported')
62+
class NegativeStepError(IndexError):
63+
def __init__(self):
64+
super().__init__("only slices with step >= 1 are supported")
8365

8466

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

8971

90-
def err_vindex_invalid_selection(selection):
91-
raise IndexError('unsupported selection type for vectorized indexing; only '
92-
'coordinate selection (tuple of integer arrays) and mask selection '
93-
'(single Boolean array) are supported; got {!r}'.format(selection))
72+
class VindexInvalidSelectionError(_BaseZarrIndexError):
73+
_msg = (
74+
"unsupported selection type for vectorized indexing; only "
75+
"coordinate selection (tuple of integer arrays) and mask selection "
76+
"(single Boolean array) are supported; got {0!r}"
77+
)

zarr/indexing.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55

66
import numpy as np
77

8-
from zarr.errors import (err_boundscheck, err_negative_step,
9-
err_too_many_indices, err_vindex_invalid_selection)
8+
from zarr.errors import (
9+
NegativeStepError,
10+
err_too_many_indices,
11+
VindexInvalidSelectionError,
12+
BoundsCheckError,
13+
)
1014

1115

1216
def is_integer(x):
@@ -46,7 +50,7 @@ def normalize_integer_selection(dim_sel, dim_len):
4650

4751
# handle out of bounds
4852
if dim_sel >= dim_len or dim_sel < 0:
49-
err_boundscheck(dim_len)
53+
raise BoundsCheckError(dim_len)
5054

5155
return dim_sel
5256

@@ -101,7 +105,7 @@ def __init__(self, dim_sel, dim_len, dim_chunk_len):
101105
# normalize
102106
self.start, self.stop, self.step = dim_sel.indices(dim_len)
103107
if self.step < 1:
104-
err_negative_step()
108+
raise NegativeStepError()
105109

106110
# store attributes
107111
self.dim_len = dim_len
@@ -385,7 +389,7 @@ def wraparound_indices(x, dim_len):
385389

386390
def boundscheck_indices(x, dim_len):
387391
if np.any(x < 0) or np.any(x >= dim_len):
388-
err_boundscheck(dim_len)
392+
raise BoundsCheckError(dim_len)
389393

390394

391395
class IntArrayDimIndexer(object):
@@ -756,7 +760,7 @@ def __getitem__(self, selection):
756760
elif is_mask_selection(selection, self.array):
757761
return self.array.get_mask_selection(selection, fields=fields)
758762
else:
759-
err_vindex_invalid_selection(selection)
763+
raise VindexInvalidSelectionError(selection)
760764

761765
def __setitem__(self, selection, value):
762766
fields, selection = pop_fields(selection)
@@ -767,7 +771,7 @@ def __setitem__(self, selection, value):
767771
elif is_mask_selection(selection, self.array):
768772
self.array.set_mask_selection(selection, value, fields=fields)
769773
else:
770-
err_vindex_invalid_selection(selection)
774+
raise VindexInvalidSelectionError(selection)
771775

772776

773777
def check_fields(fields, dtype):

zarr/storage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
from zarr.errors import (
4242
MetadataError,
43-
err_bad_compressor,
43+
BadCompressorError,
4444
ContainsArrayError,
4545
ContainsGroupError,
4646
FSPathExistNotDir,
@@ -397,8 +397,8 @@ def _init_array_metadata(
397397
if compressor:
398398
try:
399399
compressor_config = compressor.get_config()
400-
except AttributeError:
401-
err_bad_compressor(compressor)
400+
except AttributeError as e:
401+
raise BadCompressorError(compressor) from e
402402

403403
# obtain filters config
404404
if filters:

0 commit comments

Comments
 (0)