Skip to content

Commit 2d183c8

Browse files
authored
Merge pull request #782 from mattjbr123/fix_cyclic_subset_bug
Fix bug in certain cyclic subsets
2 parents e367231 + 71ecc85 commit 2d183c8

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

Changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ version NEXTVERSION
88
* Fix bug where `cf.example_fields` returned a `list`
99
of Fields rather than a `Fieldlist`
1010
(https://github.com/NCAS-CMS/cf-python/issues/725)
11+
* Fix bug where `cf.normalize_slice` doesn't correctly
12+
handle certain cyclic slices
13+
(https://github.com/NCAS-CMS/cf-python/issues/774)
1114

1215
----
1316

cf/functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,8 +2132,8 @@ def normalize_slice(index, size, cyclic=False):
21322132
return slice(start, stop, step)
21332133

21342134
if not (
2135-
(step > 0 and start < 0 and stop > 0)
2136-
or (step < 0 and start > 0 and stop < 0)
2135+
(step > 0 and start < 0 and stop >= 0)
2136+
or (step < 0 and start >= 0 and stop < 0)
21372137
):
21382138
raise IndexError(
21392139
f"{index!r} is not a {'cyclic ' if cyclic else ''}slice"

cf/test/test_functions.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,24 @@ def test_normalize_slice(self):
389389
cf.normalize_slice(slice(2, 5, -2), 8, cyclic=True),
390390
slice(2, -3, -2),
391391
)
392-
392+
393+
self.assertEqual(
394+
cf.normalize_slice(slice(-8, 0, 1), 8, cyclic=True),
395+
slice(-8, 0, 1)
396+
)
397+
self.assertEqual(
398+
cf.normalize_slice(slice(0, 7, -1), 8, cyclic=True),
399+
slice(0, -1, -1)
400+
)
401+
self.assertEqual(
402+
cf.normalize_slice(slice(-1, -8, 1), 8, cyclic=True),
403+
slice(-1, 0, 1)
404+
)
405+
self.assertEqual(
406+
cf.normalize_slice(slice(-8, -1, -1), 8, cyclic=True),
407+
slice(0, -1, -1)
408+
)
409+
393410
with self.assertRaises(IndexError):
394411
cf.normalize_slice([1, 2], 8)
395412

docs/source/contributing.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,4 @@ ideas, code, and documentation to the cf library:
126126
* Sadie Bartholomew
127127
* Thibault Hallouin
128128
* Tim Bradshaw
129+
* Matt Brown

0 commit comments

Comments
 (0)