pathwaycom/pathway · error · RuntimeError

Cannot use expression as boolean.

Error message

Cannot use expression as boolean.

What it means

ColumnExpression overrides __bool__ to raise RuntimeError because a Pathway expression is a deferred computation, not a value — Python's if/and/or/not would evaluate it immediately and almost always produce wrong results. The loud failure prevents silently using Python boolean semantics instead of building the vectorized & | ~ expression Pathway needs.

Source

Thrown at python/pathway/internals/expression.py:96

    @property
    def _table(self) -> Table:
        return self.to_column_expression()._table

    @property
    def _column(self) -> Column:
        return self.to_column_expression()._column


class ColumnExpression(OperatorInput, ABC):
    _dtype: dt.DType
    _trace: Trace

    def __init__(self):
        self._trace = Trace.from_traceback()

    def __bool__(self):
        raise RuntimeError("Cannot use expression as boolean.")

    @property
    @abstractmethod
    def _deps(self) -> tuple[ColumnExpression, ...]: ...

    @abstractmethod
    def _to_internal(self) -> InternalColExpr: ...

    def __repr__(self):
        from pathway.internals.expression_printer import ExpressionFormatter

        return ExpressionFormatter().eval_expression(self)

    @staticmethod
    def _wrap(
        arg: ColumnExpression | Value | tuple[ColumnExpression, ...]
    ) -> ColumnExpression:
        if isinstance(arg, tuple):

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Replace and/or/not with bitwise operators on expressions, each operand parenthesized: `(pw.this.a > 1) & (pw.this.b < 2)`, `~pw.this.flag`
  2. Use pw.if_else / .if_else(condition, then, else) instead of python ternaries
  3. If you truly need a per-row python branch, move the logic inside a UDF (@pw.udf) where values are materialized

Example fix

// before
table.filter(pw.this.a > 1 and pw.this.b < 2)
// after
table.filter((pw.this.a > 1) & (pw.this.b < 2))
Defensive patterns

Strategy: validation

Type guard

from pathway.internals.expression import ColumnExpression

def is_column_expression(x) -> bool:
    return isinstance(x, ColumnExpression)  # if true: never use it in if/and/or

Prevention

When it happens

Trigger: Writing `if pw.this.x:` or `if table.col > 5:`; combining conditions with `and`/`or`/`not` (e.g. `pw.this.a > 1 and pw.this.b < 2`) inside filter/select; using `expr1 or expr2` to provide a default; ternary `x if cond else y` with an expression condition.

Common situations: Translating pandas boolean masks (`df[(df.a) & (df.b)]` authors forget & needs parentheses); reusing plain-python condition logic in UDF wrappers; debugging with `if expression:` in a notebook.

Related errors


AI-assisted analysis of pathwaycom/pathway@fa2f74a464 (2026-08-15). Data as JSON: /api/errors/95fae4e89aa7da7d. Report an issue: GitHub.