{"record":{"id":"7b2daef96b04981d","repo":"pathwaycom/pathway","slug":"first-argument-of-pathway-if-else-has-to-be-bool","errorCode":null,"errorMessage":"First argument of pathway.if_else has to be bool, found {if_dtype.typehint}.","messagePattern":"First argument of pathway\\.if_else has to be bool, found (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/pathway/internals/type_interpreter.py","lineNumber":454,"sourceCode":"        expression: expr.IsNoneExpression,\n        state: TypeInterpreterState | None = None,\n        **kwargs,\n    ) -> expr.IsNoneExpression:\n        ret = super().eval_none(expression, state=state, **kwargs)\n        self._check_for_disallowed_types(\"pathway.is_none\", ret._expr._dtype)\n        return _wrap(ret, dt.BOOL)\n\n    def eval_ifelse(\n        self,\n        expression: expr.IfElseExpression,\n        state: TypeInterpreterState | None = None,\n        **kwargs,\n    ) -> expr.IfElseExpression:\n        assert state is not None\n        if_ = self.eval_expression(expression._if, state=state, **kwargs)\n        if_dtype = if_._dtype\n        if if_dtype != dt.BOOL:\n            raise TypeError(\n                f\"First argument of pathway.if_else has to be bool, found {if_dtype.typehint}.\"\n            )\n\n        if isinstance(if_, expr.IsNotNoneExpression) and isinstance(\n            if_._expr, expr.ColumnReference\n        ):\n            then_ = self.eval_expression(\n                expression._then, state=state.with_new_col([if_._expr]), **kwargs\n            )\n        else:\n            then_ = self.eval_expression(expression._then, state=state, **kwargs)\n\n        if isinstance(if_, expr.IsNoneExpression) and isinstance(\n            if_._expr, expr.ColumnReference\n        ):\n            else_ = self.eval_expression(\n                expression._else, state=state.with_new_col([if_._expr], **kwargs)\n            )","sourceCodeStart":436,"sourceCodeEnd":472,"githubUrl":"https://github.com/pathwaycom/pathway/blob/fa2f74a4649b7c5908690cf60137263d8d80de5f/python/pathway/internals/type_interpreter.py#L436-L472","documentation":"Raised when the first argument of pw.if_else is not of dtype bool. if_else(condition, then, else) requires a strict bool condition; passing an int, str, Optional[bool], or Any column as the condition fails immediately with the found typehint.","triggerScenarios":"pw.if_else(pw.this.count, a, b) where count is int; condition column typed as Optional(bool) after is_none-based logic; conditions built from comparisons whose type degraded to Any due to earlier casts; string conditions like pw.if_else(\"true\", ...).","commonSituations":"Porting Python truthiness (where non-zero/ non-empty strings are truthy) to Pathway; conditions from connectors inferred as int (0/1 flags) instead of bool; conditions that are Optional because the source can be null.","solutions":["Make the condition boolean explicitly: pw.if_else(pw.this.count > 0, a, b) or pw.this.flag == 1","For Optional[bool], handle nulls: pw.if_else(pw.this.flag.fill(True), a, b) or an is_not_none check first","If the condition is Any-typed after casts, re-establish bool with a comparison or .astype(bool)"],"exampleFix":"# before\nv = pw.if_else(pw.this.count, \"many\", \"few\")  # int condition -> TypeError\n\n# after\nv = pw.if_else(pw.this.count > 0, \"many\", \"few\")","handlingStrategy":"type-guard","validationCode":"import pathway as pw\n\ndef bool_condition(dtype) -> bool:\n    return dtype.equivalent_to(pw.typehints.Bool())\n\n# build conditions as explicit comparisons\ncond = pw.this.flag == 1  # not pw.this.flag","typeGuard":"def is_bool_dtype(dtype) -> bool:\n    import pathway as pw\n    return dtype.equivalent_to(pw.typehints.Bool())","tryCatchPattern":"try:\n    v = pw.if_else(cond, a, b)\nexcept TypeError:\n    v = pw.if_else(cond == True, a, b)  # force bool via comparison","preventionTips":["Always write conditions as explicit comparisons (==, >, is_not_none)","Declare 0/1 flag columns as bool in schemas when they are logical flags","Resolve Optional[bool] conditions with fill/is_not_none before if_else"],"tags":["pathway","type-checking","if-else","bool","typeerror"],"backgroundTag":null,"analyzedSha":"fa2f74a4649b7c5908690cf60137263d8d80de5f","analyzedAt":"2026-08-15T01:48:17.006Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}