{"record":{"id":"e212d56154072360","repo":"pola-rs/polars","slug":"cannot-compare-self-r-to-value-r","errorCode":null,"errorMessage":"cannot compare {self!r} to {value!r}","messagePattern":"cannot compare (.+?) to (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/datatype_expr/datatype_expr.py","lineNumber":78,"sourceCode":"    _pydatatype_expr: PyDataTypeExpr = None  # type: ignore[assignment]\n    _accessors: ClassVar[set[str]] = {\n        \"arr\",\n        \"enum\",\n        \"list\",\n        \"struct\",\n    }\n\n    def __eq__(self, value: PolarsDataType | DataTypeExpr) -> pl.Expr:  # type: ignore[override]\n        cmp_with: DataTypeExpr\n        if isinstance(value, pl.DataType):\n            cmp_with = value.to_dtype_expr()\n        elif isinstance(value, pl.DataTypeClass):\n            cmp_with = value.to_dtype_expr()\n        elif isinstance(value, DataTypeExpr):\n            cmp_with = value\n        else:\n            msg = f\"cannot compare {self!r} to {value!r}\"\n            raise TypeError(msg) from None\n\n        return pl.Expr._from_pyexpr(\n            self._pydatatype_expr.equals(cmp_with._pydatatype_expr)\n        )\n\n    def __ne__(self, value: PolarsDataType | DataTypeExpr) -> pl.Expr:  # type: ignore[override]\n        return (self == value).not_()\n\n    @classmethod\n    def _from_pydatatype_expr(cls, pydatatype_expr: PyDataTypeExpr) -> DataTypeExpr:\n        slf = cls()\n        slf._pydatatype_expr = pydatatype_expr\n        return slf\n\n    def inner_dtype(self) -> DataTypeExpr:\n        \"\"\"Get the inner DataType of a List or Array.\"\"\"\n        return DataTypeExpr._from_pydatatype_expr(self._pydatatype_expr.inner_dtype())\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/datatype_expr/datatype_expr.py#L60-L96","documentation":"`DataTypeExpr.__eq__` builds a runtime expression comparing a data-type expression to another dtype expression. Only `pl.DataType` classes/instances and `DataTypeExpr` values are supported; comparing to anything else (e.g. a raw string) raises this TypeError because the comparison cannot be compiled.","triggerScenarios":"`expr.dtype.__eq__(\"Int64\")` or comparing a DataTypeExpr to a Python type like `int`, `str`, `list`, or an unrelated object.","commonSituations":"Developers pass string dtype names (`'Int64'`) or builtin types (`int`) where a Polars dtype class such as `pl.Int64` is required, often coming from JSON config or dynamic code.","solutions":["Pass a Polars dtype class or instance: `pl.Int64` instead of `\"Int64\"` or `int`","Convert the value first with `pl.DataType` constructors or `pl.datatypes` helpers","Use `DataTypeExpr` objects if comparing expression-to-expression"],"exampleFix":"// before\npl.col('a').dtype == 'Int64'\n// after\npl.col('a').dtype == pl.Int64","handlingStrategy":"type-guard","validationCode":"def ensure_dtype_like(v):\n    import polars as pl\n    if not (isinstance(v, DataTypeExpr) or (isinstance(v, type) and issubclass(v, pl.DataType))):\n        raise TypeError(f'expected polars dtype, got {v!r}')\n    return v","typeGuard":"def is_dtype_like(v) -> bool:\n    import polars as pl\n    from polars.datatype_expr import DataTypeExpr\n    return isinstance(v, DataTypeExpr) or (isinstance(v, type) and issubclass(v, pl.DataType))","tryCatchPattern":"try:\n    result = expr == candidate\nexcept TypeError:\n    candidate = POLARS_DTYPE_MAP.get(candidate, pl.AutoDtype)\n    result = expr == candidate","preventionTips":["Always compare against pl.* dtype classes, never strings or builtins","Map string dtype names through a lookup table before comparing"],"tags":["polars","datatype","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}