Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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, -1, -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
1 change: 1 addition & 0 deletions docs/source/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,4 @@ ideas, code, and documentation to the cf library:
* Sadie Bartholomew
* Thibault Hallouin
* Tim Bradshaw
* Matt Brown