{"record":{"id":"84a0b857c1b7f626","repo":"pathwaycom/pathway","slug":"type-of-default-default-type-typehint-is-not-com","errorCode":null,"errorMessage":"type of default {default_type.typehint} is not compatible with {target_type.typehint}.","messagePattern":"type of default (.+?) is not compatible with (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/pathway/internals/type_interpreter.py","lineNumber":344,"sourceCode":"        self,\n        expression: expr.ConvertExpression,\n        state: TypeInterpreterState | None = None,\n        **kwargs,\n    ) -> expr.ConvertExpression:\n        expression = super().eval_convert(expression, state=state, **kwargs)\n        target_type = expression._return_type\n        default_type = expression._default._dtype\n\n        if dt.unoptionalize(expression._expr._dtype) is not dt.JSON:\n            raise TypeError(\n                f\"{expression!r} can only be applied to JSON columns, \"\n                f\"but column has type {expression._expr._dtype.typehint}.\"\n            )\n\n        if default_type != dt.NONE and not dt.dtype_issubclass(\n            dt.unoptionalize(default_type), target_type\n        ):\n            raise TypeError(\n                f\"type of default {default_type.typehint} \"\n                f\"is not compatible with {target_type.typehint}.\"\n            )\n\n        if not expression._unwrap and (\n            isinstance(default_type, dt.Optional) or default_type == dt.NONE\n        ):\n            target_type = dt.Optional(target_type)\n\n        return _wrap(expression, target_type)\n\n    def eval_declare(\n        self,\n        expression: expr.DeclareTypeExpression,\n        state: TypeInterpreterState | None = None,\n        **kwargs,\n    ) -> expr.DeclareTypeExpression:\n        expression = super().eval_declare(expression, state=state, **kwargs)","sourceCodeStart":326,"sourceCodeEnd":362,"githubUrl":"https://github.com/pathwaycom/pathway/blob/fa2f74a4649b7c5908690cf60137263d8d80de5f/python/pathway/internals/type_interpreter.py#L326-L362","documentation":"Raised by the type interpreter when pw.unwrap(...) is given a default whose dtype is not a subtype of the conversion's target return type. unwrap(default=...) must return a value compatible with the type the JSON is converted to; e.g. unwrapping a JSON field typed as int with default=\"\" (str default) fails this check.","triggerScenarios":"pw.this.col.unwrap(default=\"none\") where the JSON field's converted type is int/float; unwrap(default=0) on a field declared to unwrap into str; passing a bool default for a str field.","commonSituations":"Copy-pasting defaults between unwrap calls with different field types; assuming defaults are untyped sentinels; defaults used to mask missing keys without matching the field's JSON schema.","solutions":["Match the default's Python type to the field's converted type: unwrap(default=0) for int fields, unwrap(default=\"\") for str fields","Use None (with optional=... semantics) as default, which is always allowed since None-typed defaults are skipped by the check","Re-check the JSON schema of the field (what unwrap will produce) and align the default literal"],"exampleFix":"# before\nv = pw.this.payload.unwrap(default=\"\")  # field unwraps to int\n\n# after\nv = pw.this.payload.unwrap(default=0)\n# or\nv = pw.this.payload.unwrap(default=None)","handlingStrategy":"validation","validationCode":"import pathway as pw\n\ndef default_matches(default, target_type_example):\n    return default is None or type(default) is type(target_type_example)\n\n# e.g. only pass default=0 when the field unwraps to int\ndefault = 0 if field_is_int else (\"\" if field_is_str else None)","typeGuard":"def default_ok(default, python_type) -> bool:\n    return default is None or isinstance(default, python_type)","tryCatchPattern":"try:\n    v = pw.this.payload.unwrap(default=0)\nexcept TypeError:\n    v = pw.this.payload.unwrap(default=None)  # always-allowed default","preventionTips":["Default to None for unwrap defaults unless a typed fallback is required","Match default literal type to the field's converted type by convention","Document each JSON field's expected type next to the unwrap call"],"tags":["pathway","type-checking","unwrap","default-value","typeerror"],"backgroundTag":null,"analyzedSha":"fa2f74a4649b7c5908690cf60137263d8d80de5f","analyzedAt":"2026-08-15T01:48:17.006Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}