{"record":{"id":"c2a2043a9979f519","repo":"pathwaycom/pathway","slug":"cannot-perform-pathway-if-else-on-columns-of-types","errorCode":null,"errorMessage":"Cannot perform pathway.if_else on columns of types {then_dtype.typehint} and {else_dtype.typehint}.","messagePattern":"Cannot perform pathway\\.if_else on columns of types (.+?) and (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/pathway/internals/type_interpreter.py","lineNumber":481,"sourceCode":"            )\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            )\n        else:\n            else_ = self.eval_expression(expression._else, state=state, **kwargs)\n\n        then_dtype = then_._dtype\n        else_dtype = else_._dtype\n        try:\n            lca = dt.types_lca(then_dtype, else_dtype, raising=True)\n        except TypeError:\n            raise TypeError(\n                f\"Cannot perform pathway.if_else on columns of types {then_dtype.typehint} and {else_dtype.typehint}.\"\n            )\n        if lca is dt.ANY:\n            raise TypeError(\n                f\"Cannot perform pathway.if_else on columns of types {then_dtype.typehint} and {else_dtype.typehint}.\"\n            )\n        expression = expr.IfElseExpression(if_, then_, else_)\n        return _wrap(expression, lca)\n\n    def eval_make_tuple(\n        self,\n        expression: expr.MakeTupleExpression,\n        state: TypeInterpreterState | None = None,\n        **kwargs,\n    ) -> expr.MakeTupleExpression:\n        expression = super().eval_make_tuple(expression, state=state, **kwargs)\n        dtypes = tuple(arg._dtype for arg in expression._args)\n        self._check_for_disallowed_types(\"pathway.make_tuple\", *dtypes)","sourceCodeStart":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/pathwaycom/pathway/blob/fa2f74a4649b7c5908690cf60137263d8d80de5f/python/pathway/internals/type_interpreter.py#L463-L499","documentation":"Raised when the then/else branches of pw.if_else have dtypes with no common ancestor: dt.types_lca(then, else, raising=True) throws and the interpreter converts it into a TypeError naming both typehints. Both branches must unify to one result dtype.","triggerScenarios":"pw.if_else(cond, pw.this.int_col, pw.this.str_col); branches of different literal types (1 vs \"1\"); one branch returning DateTime and the other None-untyped; branches whose LCA only exists for identical shapes of tuples/lists and those shapes differ.","commonSituations":"Returning type-inconsistent literals from branches (0 vs None vs \"\"); joining values from columns with divergent schemas; forgetting to cast one branch after a schema change.","solutions":["Cast both branches to a common type: pw.if_else(cond, pw.this.a.astype(str), pw.this.b.astype(str))","Use matching literal types in both branches (e.g. None handled via Optional-aware expressions or equal-typed sentinels)","Align source schemas so paired columns share dtype","If mixing is deliberate, cast both to Any (documented escape hatch, loses checking)"],"exampleFix":"# before\nv = pw.if_else(pw.this.ok, pw.this.code, pw.this.label)  # int vs str\n\n# after\nv = pw.if_else(pw.this.ok, pw.this.code.astype(str), pw.this.label)","handlingStrategy":"type-guard","validationCode":"import pathway as pw\n\ndef branches_compatible(then_dtype, else_dtype) -> bool:\n    try:\n        lca = pw.typehints.lca(then_dtype, else_dtype)\n        return lca is not None and not lca.equivalent_to(pw.typehints.Any())\n    except TypeError:\n        return False","typeGuard":"def branches_same_type(then_dtype, else_dtype) -> bool:\n    return then_dtype.equivalent_to(else_dtype)","tryCatchPattern":"try:\n    v = pw.if_else(cond, x, y)\nexcept TypeError:\n    v = pw.if_else(cond, x.astype(str), y.astype(str))","preventionTips":["Keep then/else branch types identical by construction","Use same-typed sentinels (0 / 0.0, \"\" / \"\") instead of mixing types","Cast one branch when pairing columns from different schemas"],"tags":["pathway","type-checking","if-else","dtype","typeerror"],"backgroundTag":null,"analyzedSha":"fa2f74a4649b7c5908690cf60137263d8d80de5f","analyzedAt":"2026-08-15T01:48:17.006Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}