{"record":{"id":"76b26301f4b0ca8f","repo":"redis/redis-py","slug":"item-not-found","errorCode":null,"errorMessage":"Item not found","messagePattern":"Item not found","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"redis/data_structure.py","lineNumber":39,"sourceCode":"            # Find insertion point using binary search\n            left, right = 0, len(self._items)\n            while left < right:\n                mid = (left + right) // 2\n                if self._items[mid][1] < weight:\n                    right = mid\n                else:\n                    left = mid + 1\n\n            self._items.insert(left, (item, weight))\n\n    def remove(self, item):\n        \"\"\"Remove first occurrence of item\"\"\"\n        with self._lock:\n            for i, (stored_item, weight) in enumerate(self._items):\n                if stored_item == item:\n                    self._items.pop(i)\n                    return weight\n            raise ValueError(\"Item not found\")\n\n    def get_by_weight_range(\n        self, min_weight: float, max_weight: float\n    ) -> List[tuple[Any, Number]]:\n        \"\"\"Get all items within weight range\"\"\"\n        with self._lock:\n            result = []\n            for item, weight in self._items:\n                if min_weight <= weight <= max_weight:\n                    result.append((item, weight))\n            return result\n\n    def get_top_n(self, n: int) -> List[tuple[Any, Number]]:\n        \"\"\"Get top N the highest weighted items\"\"\"\n        with self._lock:\n            return [(item, weight) for item, weight in self._items[:n]]\n\n    def update_weight(self, item, new_weight: float):","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/data_structure.py#L21-L57","documentation":"WeightedList.remove (redis/data_structure.py:39) raises a plain ValueError('Item not found') when the item being removed is not present in the internal sorted list. WeightedList is a thread-safe weighted container used internally by the cluster client (e.g. for weighted primary/replica selection); calling remove on an item that was never added, already removed, or passed with a different identity/value raises this.","triggerScenarios":"Calling WeightedList.remove(item) twice; removing an item that was never add()'ed; passing an object whose __eq__ does not match the stored instance. Also reached transitively through update_weight, which calls remove internally, for a stale item.","commonSituations":"Mostly an internal-API error seen by contributors extending routing/replica selection. End users essentially never call WeightedList directly; it surfaces as an unexpected ValueError during cluster topology refresh or replica routing if the slot map and weighted list fall out of sync after a reshard/failover.","solutions":["If calling WeightedList directly, guard with a membership check (iterate, since it has no __contains__) before remove, or catch ValueError.","For update_weight, ensure the item you are re-weighting was added with the exact same identity.","In cluster code, treat a stray ValueError here as a topology-staleness signal and trigger a slot-map refresh rather than crashing."],"exampleFix":"// before\nwl.remove(node)  # ValueError if already removed\n// after\nif any(stored is node for stored, _ in wl):\n    wl.remove(node)","handlingStrategy":"try-catch","validationCode":"def safe_remove(wl, item):\n    if any(stored is item or stored == item for stored, _ in wl):\n        return wl.remove(item)\n    return None","typeGuard":null,"tryCatchPattern":"try:\n    wl.remove(item)\nexcept ValueError:\n    pass  # already absent","preventionTips":["Treat a stray ValueError from WeightedList during cluster routing as a topology-staleness signal and refresh the slot map.","When calling update_weight, ensure the item identity matches what was add()'ed.","Avoid calling remove twice on the same item."],"tags":["data-structure","internal","cluster-routing","valueerror"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}