diff options
Diffstat (limited to '')
-rw-r--r-- | polymatrix/expression/init.py | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/polymatrix/expression/init.py b/polymatrix/expression/init.py index f0c2a0b..6f75ab3 100644 --- a/polymatrix/expression/init.py +++ b/polymatrix/expression/init.py @@ -394,9 +394,22 @@ def init_slice_expr( underlying: ExpressionBaseMixin, slices: int | slice | range | tuple[int | slice | range, int | slice | range] ): - # FIXME: For some reason in older python versions slice is not a hashable - # type and this causes crashes when old code uses the cache in the state object - # cf. https://stackoverflow.com/questions/29980786/why-are-slice-objects-not-hashable-in-python + # FIXME: see comment above this HashableSlice class + HashableSlice = polymatrix.expression.mixins.sliceexprmixin.HashableSlice + + if isinstance(slices, slice): + slices = HashableSlice(slices.start, slices.stop, slices.step) + + elif isinstance(slices, tuple): + new_slices = list(slices) + if isinstance(slices[0], slice): + new_slices[0] = HashableSlice(slices[0].start, slices[0].stop, slices[0].step) + + if isinstance(slices[1], slice): + new_slices[1] = HashableSlice(slices[1].start, slices[1].stop, slices[1].step) + + slices = tuple(new_slices) + return polymatrix.expression.impl.SliceExprImpl(underlying, slices) |