Skip to content

Commit

Permalink
Merge pull request #782 from mattjbr123/fix_cyclic_subset_bug
Browse files Browse the repository at this point in the history
Fix bug in certain cyclic subsets
  • Loading branch information
davidhassell authored Jun 11, 2024
2 parents e367231 + 71ecc85 commit 2d183c8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ version NEXTVERSION
* 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

0 comments on commit 2d183c8

Please sign in to comment.