{"record":{"id":"f2da204314bf393f","repo":"pola-rs/polars","slug":"the-length-of-value-name-len-values-does","errorCode":null,"errorMessage":"the length of `{value_name}` ({len(values)}) does not match the length of `{match_name}` ({n_match})","messagePattern":"the length of `(.+?)` \\((.+?)\\) does not match the length of `(.+?)` \\((.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/various.py","lineNumber":563,"sourceCode":"        del stack_frame\n\n    return objects\n\n\ndef extend_bool(\n    value: bool | Sequence[bool],  # noqa: FBT001\n    n_match: int,\n    value_name: str,\n    match_name: str,\n) -> Sequence[bool]:\n    \"\"\"Ensure the given bool or sequence of bools is the correct length.\"\"\"\n    values = [value] * n_match if isinstance(value, bool) else value\n    if n_match != len(values):\n        msg = (\n            f\"the length of `{value_name}` ({len(values)}) \"\n            f\"does not match the length of `{match_name}` ({n_match})\"\n        )\n        raise ValueError(msg)\n    return values\n\n\ndef in_terminal_that_supports_colour() -> bool:\n    \"\"\"\n    Determine (within reason) if we are in an interactive terminal that supports color.\n\n    Note: this is not exhaustive, but it covers a lot (most?) of the common cases.\n    \"\"\"\n    if hasattr(sys.stdout, \"isatty\"):\n        # can enhance as necessary, but this is a reasonable start\n        return (\n            sys.stdout.isatty()\n            and (\n                sys.platform != \"win32\"\n                or \"ANSICON\" in os.environ\n                or \"WT_SESSION\" in os.environ\n                or os.environ.get(\"TERM_PROGRAM\") == \"vscode\"","sourceCodeStart":545,"sourceCodeEnd":581,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/various.py#L545-L581","documentation":"ValueError from extend_bool (py-polars/src/polars/_utils/various.py:550-564). Sort/top-k style APIs accept per-column boolean flags either as a single bool (broadcast to all columns) or as a sequence with exactly one entry per key column. extend_bool broadcasts the scalar or validates the sequence length against len(by)/len(exprs) and names both sides in the message, e.g. the length of `descending` (3) does not match the length of `by` (2).","triggerScenarios":"LazyFrame/Expr.sort_by(by=['a','b'], descending=[True, False, True]); pl.top_k_by style calls in polars/functions/lazy.py with descending/nulls_last lists longer or shorter than exprs; Series/DataFrame methods that route through require_same_type callers of extend_bool with 'reverse' vs 'by' (Expr.sort_by(reverse=...)).","commonSituations":"Adding a sort key and forgetting to extend the descending/nulls_last lists; building the flags list from a different source than the column list (stale config dict); passing a single-element list while sorting by multiple columns.","solutions":["Pass a scalar to apply to all columns: descending=True","Make the list length equal len(by): one bool per sort/expr key","Derive flags from the key list: descending=[cfg[c] for c in by_cols]"],"exampleFix":"# before\nlf.sort_by(['a', 'b'], descending=[True, False, True])  # ValueError\n\n# after\nlf.sort_by(['a', 'b'], descending=[True, False])","handlingStrategy":"validation","validationCode":"def norm_flags(by: list, flags: bool | list[bool]) -> list[bool]:\n    if isinstance(flags, bool):\n        return [flags] * len(by)\n    if len(flags) != len(by):\n        raise ValueError(f'need {len(by)} flags, got {len(flags)}')\n    return list(flags)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass a scalar bool when all columns share the same flag","Derive flag lists from the key list itself ([cfg[c] for c in by_cols])","Add a unit check in helpers that forward descending/nulls_last/reverse to polars sort APIs"],"tags":["polars","sort-by","length-mismatch","valueerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}