diff --git a/src/delayedarray/RegularTicks.py b/src/delayedarray/RegularTicks.py index b622ae2..b037373 100644 --- a/src/delayedarray/RegularTicks.py +++ b/src/delayedarray/RegularTicks.py @@ -43,8 +43,10 @@ def __getitem__(self, i: int) -> int: Returns: Position of tick ``i``. """ - if i >= self._len: - raise IndexError("'i' is out of range") - elif i < 0: + if i < 0: i += self._len + if i < 0: + raise IndexError("'i' is out of range") + elif i >= self._len: + raise IndexError("'i' is out of range") return min(self._final, self._spacing * (i + 1)) diff --git a/tests/test_RegularTicks.py b/tests/test_RegularTicks.py index 32ea3de..9c73560 100644 --- a/tests/test_RegularTicks.py +++ b/tests/test_RegularTicks.py @@ -1,4 +1,5 @@ import delayedarray +import pytest def test_RegularTicks(): @@ -8,6 +9,11 @@ def test_RegularTicks(): assert list(out) == ref assert out[-1] == 50 # check reverse indexing. + with pytest.raises(IndexError, match="out of range") as ex: + out[-10] + with pytest.raises(IndexError, match="out of range") as ex: + out[10] + out = delayedarray.RegularTicks(10, 50) ref = list(range(10, 51, 10)) assert list(out) == ref