pola-rs/polars · error · TypeError

at least one predicate or constraint must be provided

Error message

at least one predicate or constraint must be provided

What it means

_combine_predicates (parse/expr.py:304) requires at least one predicate across the *predicates and **constraints accepted by pl.when(...), Expr.when(...), and Expr.filter(...). With zero inputs there is nothing to AND together, so it raises this TypeError rather than producing a meaningless expression that would fail later.

Source

Thrown at py-polars/src/polars/_utils/parse/expr.py:304

    """
    all_predicates = _parse_positional_inputs(predicates)  # type: ignore[arg-type]

    if constraints:
        constraint_predicates = _parse_constraints(constraints)
        all_predicates.extend(constraint_predicates)

    return _combine_predicates(all_predicates)


def _parse_constraints(constraints: dict[str, IntoExpr]) -> Iterable[PyExpr]:
    for name, value in constraints.items():
        yield F.col(name).eq(value)._pyexpr


def _combine_predicates(predicates: list[PyExpr]) -> PyExpr:
    if not predicates:
        msg = "at least one predicate or constraint must be provided"
        raise TypeError(msg)

    if len(predicates) == 1:
        return predicates[0]

    return plr.all_horizontal(predicates)

View on GitHub (pinned to df599052da)

Solutions

  1. Default to a neutral predicate: preds = predicates or [pl.lit(True)] before pl.when(*preds)
  2. Skip the when/otherwise chain entirely when no predicates apply
  3. Validate at the API boundary: require at least one predicate or constraint

Example fix

# before
pl.when(*predicates).then(x).otherwise(y)  # predicates == []

# after
predicates = predicates or [pl.lit(True)]
pl.when(*predicates).then(x).otherwise(y)
Defensive patterns

Strategy: validation

Validate before calling

if not predicates and not constraints:
    raise TypeError("at least one predicate or constraint must be provided")
pl.when(*predicates, **constraints)

Type guard

def has_any_predicate(predicates, constraints) -> bool:
    return len(predicates) > 0 or len(constraints) > 0

Prevention

When it happens

Trigger: pl.when() called with no arguments; dynamically assembled predicate lists that end up empty: pl.when(*preds) with preds == []; expr.filter() with no predicates; when(**constraints) with an empty dict.

Common situations: Config/user-driven predicate builders; helper functions forwarding *args into when()/filter(); branches where all optional filters are absent.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/6214ccee128fdfad. Report an issue: GitHub.