{"record":{"id":"ca6af0ab58b914bd","repo":"reflex-dev/reflex","slug":"slice-step-cannot-be-zero","errorCode":null,"errorMessage":"slice step cannot be zero","messagePattern":"slice step cannot be zero","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/vars/sequence.py","lineNumber":1658,"sourceCode":"        \"\"\"\n        start, end, step = self._start, self._stop, self._step\n\n        normalized_start = (\n            LiteralVar.create(start) if start is not None else Var(_js_expr=\"undefined\")\n        )\n        normalized_end = (\n            LiteralVar.create(end) if end is not None else Var(_js_expr=\"undefined\")\n        )\n        if step is None:\n            return f\"{self._array!s}.slice({normalized_start!s}, {normalized_end!s})\"\n        if not isinstance(step, Var):\n            if step < 0:\n                actual_start = end + 1 if end is not None else 0\n                actual_end = start + 1 if start is not None else self._array.length()\n                return str(self._array[actual_start:actual_end].reverse()[::-step])\n            if step == 0:\n                msg = \"slice step cannot be zero\"\n                raise ValueError(msg)\n            return f\"{self._array!s}.slice({normalized_start!s}, {normalized_end!s}).filter((_, i) => i % {step!s} === 0)\"\n\n        actual_start_reverse = end + 1 if end is not None else 0\n        actual_end_reverse = start + 1 if start is not None else self._array.length()\n\n        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)\"\n\n    @classmethod\n    def create(\n        cls,\n        array: ArrayVar,\n        slice: slice,\n        _var_data: VarData | None = None,\n    ) -> ArraySliceOperation:\n        \"\"\"Create a var from a string value.\n\n        Args:\n            array: The array.","sourceCodeStart":1640,"sourceCodeEnd":1676,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/vars/sequence.py#L1640-L1676","documentation":"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'.","triggerScenarios":"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.","commonSituations":"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 [::].","solutions":["Guard the step: default it to 1 when it computes to 0.","Fix the literal step in the slice expression to a non-zero value (positive or negative).","Validate numeric input before using it as a step."],"exampleFix":"# before\nrx.text(State.items[::0])\n\n# after\nstep = step or 1\nrx.text(State.items[::step])","handlingStrategy":"validation","validationCode":"def valid_step(step) -> bool:\n    return step is None or (isinstance(step, int) and step != 0)","typeGuard":"def nonzero_step(s) -> bool:\n    return s is None or (isinstance(s, int) and s != 0)","tryCatchPattern":null,"preventionTips":["Coalesce dynamic steps: step = step or 1.","Validate user-provided step values before slicing.","Prefer positive steps unless reverse iteration is required."],"tags":["reflex","arrayvar","slice","step-zero"],"backgroundTag":"slice-step-zero","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}