{"record":{"id":"118c5827e8557602","repo":"pola-rs/polars","slug":"reduce-balanced-of-empty-iterable","errorCode":null,"errorMessage":"reduce_balanced() of empty iterable","messagePattern":"reduce_balanced\\(\\) of empty iterable","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/reduce_balanced.py","lineNumber":13,"sourceCode":"from collections.abc import Callable, Iterable\nfrom typing import TypeVar\n\nT = TypeVar(\"T\")\n\n\ndef reduce_balanced(function: Callable[[T, T], T], iterable: Iterable[T]) -> T:\n    \"\"\"Applies a reduction in a balanced tree pattern.\"\"\"\n    values = list(iterable)\n\n    if not values:\n        msg = \"reduce_balanced() of empty iterable\"\n        raise TypeError(msg)\n\n    if len(values) == 1:\n        return values.pop()\n\n    stack = [(0, len(values))]\n\n    i = 0\n\n    while i < len(stack):\n        offset, length = stack[i]\n        half = -(length // -2)\n\n        if length > 3:\n            stack.append((offset + half, length - half))\n\n        if length > 2:\n            stack.append((offset, half))\n","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/pola-rs/polars/blob/9b5d73fd00236295624374b075d16b1fe6ec6df9/py-polars/src/polars/_utils/reduce_balanced.py#L1-L31","documentation":"TypeError raised by reduce_balanced: the input iterable was empty, so there is no initial value to seed the balanced-tree reduction. It mirrors Python's built-in reduce behavior of refusing to reduce an empty sequence without an initializer.","triggerScenarios":"reduce_balanced called with an empty iterable.","commonSituations":"See trigger scenarios.","solutions":["Pass a non-empty iterable to reduce_balanced, or handle the empty case before calling."],"exampleFix":"if items: reduce_balanced(items)","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"9b5d73fd00236295624374b075d16b1fe6ec6df9","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}