Skip to content

Commit

Permalink
Refactor _to_slice function to handle slice start/stop/step values th…
Browse files Browse the repository at this point in the history
…at are not integers
  • Loading branch information
tlambert03 committed Oct 16, 2024
1 parent 87ee84a commit 069fdc8
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ndv/models/_array_display_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ def _maybe_int(val: Any) -> Any:
def _to_slice(val: Any) -> slice:

Check warning on line 34 in src/ndv/models/_array_display_model.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/models/_array_display_model.py#L34

Added line #L34 was not covered by tests
# slices are returned as is
if isinstance(val, slice):
if not all(

Check warning on line 37 in src/ndv/models/_array_display_model.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/models/_array_display_model.py#L36-L37

Added lines #L36 - L37 were not covered by tests
isinstance(i, (int, type(None))) for i in (val.start, val.stop, val.step)
):
raise TypeError(f"Slice start/stop/step must all be integers: {val!r}")
return val

Check warning on line 41 in src/ndv/models/_array_display_model.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/models/_array_display_model.py#L40-L41

Added lines #L40 - L41 were not covered by tests
# single integers are converted to slices starting at that index
if isinstance(val, int):
return slice(val, val + 1)

Check warning on line 44 in src/ndv/models/_array_display_model.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/models/_array_display_model.py#L43-L44

Added lines #L43 - L44 were not covered by tests
# sequences are interpreted as arguments to the slice constructor
if isinstance(val, Sequence) and not isinstance(val, str):
if isinstance(val, Sequence):
return slice(*(int(x) if x is not None else None for x in val))
raise TypeError(f"Expected int or slice, got {type(val)}")

Check warning on line 48 in src/ndv/models/_array_display_model.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/models/_array_display_model.py#L46-L48

Added lines #L46 - L48 were not covered by tests

Expand Down

0 comments on commit 069fdc8

Please sign in to comment.