reflex-dev/reflex · error · ValueError

slice step cannot be zero

Error message

slice step cannot be zero

What it means

When Reflex compiles a slice operation on an array var into JS (.slice(...).filter(...)), a step of 0 would cause an infinite/empty filter (i % 0), so it is rejected upfront with ValueError, mirroring Python's own 'slice step cannot be zero'.

Source

Thrown at packages/reflex-base/src/reflex_base/vars/sequence.py:1658

        """
        start, end, step = self._start, self._stop, self._step

        normalized_start = (
            LiteralVar.create(start) if start is not None else Var(_js_expr="undefined")
        )
        normalized_end = (
            LiteralVar.create(end) if end is not None else Var(_js_expr="undefined")
        )
        if step is None:
            return f"{self._array!s}.slice({normalized_start!s}, {normalized_end!s})"
        if not isinstance(step, Var):
            if step < 0:
                actual_start = end + 1 if end is not None else 0
                actual_end = start + 1 if start is not None else self._array.length()
                return str(self._array[actual_start:actual_end].reverse()[::-step])
            if step == 0:
                msg = "slice step cannot be zero"
                raise ValueError(msg)
            return f"{self._array!s}.slice({normalized_start!s}, {normalized_end!s}).filter((_, i) => i % {step!s} === 0)"

        actual_start_reverse = end + 1 if end is not None else 0
        actual_end_reverse = start + 1 if start is not None else self._array.length()

        return f"{self.step!s} > 0 ? {self._array!s}.slice({normalized_start!s}, {normalized_end!s}).filter((_, i) => i % {step!s} === 0) : {self._array!s}.slice({actual_start_reverse!s}, {actual_end_reverse!s}).reverse().filter((_, i) => i % {-step!s} === 0)"

    @classmethod
    def create(
        cls,
        array: ArrayVar,
        slice: slice,
        _var_data: VarData | None = None,
    ) -> ArraySliceOperation:
        """Create a var from a string value.

        Args:
            array: The array.

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Guard the step: default it to 1 when it computes to 0.
  2. Fix the literal step in the slice expression to a non-zero value (positive or negative).
  3. Validate numeric input before using it as a step.

Example fix

# before
rx.text(State.items[::0])

# after
step = step or 1
rx.text(State.items[::step])
Defensive patterns

Strategy: validation

Validate before calling

def valid_step(step) -> bool:
    return step is None or (isinstance(step, int) and step != 0)

Type guard

def nonzero_step(s) -> bool:
    return s is None or (isinstance(s, int) and s != 0)

Prevention

When it happens

Trigger: Using a zero step when slicing a Reflex array var: `State.items[::0]`, `State.items[start:end:0]`, or a computed step value that evaluates to 0.

Common situations: Dynamic slice steps from user input or config where step can be 0; copy-paste of range logic where 0 slipped in; typos like [::0] instead of [::].

Related errors


AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28). Data as JSON: /api/errors/ca6af0ab58b914bd. Report an issue: GitHub.