From 069fdc89c3652f6633789039f88b02e208019ce9 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Wed, 16 Oct 2024 15:00:22 -0400 Subject: [PATCH] Refactor _to_slice function to handle slice start/stop/step values that are not integers --- src/ndv/models/_array_display_model.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ndv/models/_array_display_model.py b/src/ndv/models/_array_display_model.py index aff40f7..3ecc91d 100644 --- a/src/ndv/models/_array_display_model.py +++ b/src/ndv/models/_array_display_model.py @@ -34,12 +34,16 @@ def _maybe_int(val: Any) -> Any: def _to_slice(val: Any) -> slice: # slices are returned as is if isinstance(val, slice): + if not all( + 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 # single integers are converted to slices starting at that index if isinstance(val, int): return slice(val, val + 1) # 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)}")