Skip to content

Commit

Permalink
Modified normalize_slice in cf/functions.py to fix #774
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjbr123 committed Jun 7, 2024
1 parent 7a09fef commit 7116b1d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ version NEXTRELEASE
* Fix bug where `cf.example_fields` returned a `list`
of Fields rather than a `Fieldlist`
(https://github.com/NCAS-CMS/cf-python/issues/725)
* Fix bug where `cf.functions.normalize_slice` doesn't correctly
handle certain cyclic slices
(https://github.com/NCAS-CMS/cf-python/issues/774)

----

Expand Down
4 changes: 2 additions & 2 deletions cf/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2132,8 +2132,8 @@ def normalize_slice(index, size, cyclic=False):
return slice(start, stop, step)

if not (
(step > 0 and start < 0 and stop > 0)
or (step < 0 and start > 0 and stop < 0)
(step > 0 and start < 0 and stop >= 0)
or (step < 0 and start >= 0 and stop < 0)
):
raise IndexError(
f"{index!r} is not a {'cyclic ' if cyclic else ''}slice"
Expand Down
19 changes: 18 additions & 1 deletion cf/test/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,24 @@ def test_normalize_slice(self):
cf.normalize_slice(slice(2, 5, -2), 8, cyclic=True),
slice(2, -3, -2),
)


self.assertEqual(
cf.normalize_slice(slice(-8, 0, 1), 8, cyclic=True),
slice(-8, 0, 1)
)
self.assertEqual(
cf.normalize_slice(slice(0, 7, -1), 8, cyclic=True),
slice(0, 7, -1)
)
self.assertEqual(
cf.normalize_slice(slice(-1, -8, 1), 8, cyclic=True),
slice(-1, 0, 1)
)
self.assertEqual(
cf.normalize_slice(slice(-8, -1, -1), 8, cyclic=True),
slice(0, -1, -1)
)

with self.assertRaises(IndexError):
cf.normalize_slice([1, 2], 8)

Expand Down

0 comments on commit 7116b1d

Please sign in to comment.