{"record":{"id":"89985e7be6cf9c6d","repo":"pola-rs/polars","slug":"dtype-dtype-is-a-baseextension-class-it-shoul","errorCode":null,"errorMessage":"dtype '{dtype}' is a BaseExtension class, it should be an instance","messagePattern":"dtype '(.+?)' is a BaseExtension class, it should be an instance","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/lit.py","lineNumber":89,"sourceCode":"    >>> pl.lit(datetime(2023, 3, 31, 10, 30, 45))  # doctest: +IGNORE_RESULT\n\n    Literal list/Series data (1D):\n\n    >>> pl.lit([1, 2, 3])  # doctest: +SKIP\n    >>> pl.lit(pl.Series(\"x\", [1, 2, 3]))  # doctest: +IGNORE_RESULT\n\n    Literal list/Series data (2D):\n\n    >>> pl.lit([[1, 2], [3, 4]])  # doctest: +SKIP\n    >>> pl.lit(pl.Series(\"y\", [[1, 2], [3, 4]]))  # doctest: +IGNORE_RESULT\n    \"\"\"\n    time_unit: TimeUnit\n\n    if isinstance(dtype, BaseExtension):\n        return lit(value, dtype.ext_storage()).ext.to(dtype)\n    elif isinstance(dtype, type) and issubclass(dtype, BaseExtension):\n        msg = f\"dtype '{dtype}' is a BaseExtension class, it should be an instance\"\n        raise TypeError(msg)\n    elif isinstance(dtype, DataTypeExpr):\n        return lit(value).cast(dtype)\n    elif dtype == Object:\n        value_s = pl.Series(\"literal\", [value], dtype=dtype)\n        return wrap_expr(plr.lit(value_s._s, allow_object, is_scalar=True))\n\n    if isinstance(value, datetime):\n        if dtype == Date:\n            return wrap_expr(plr.lit(value.date(), allow_object=False, is_scalar=True))\n\n        # parse time unit\n        if dtype is not None and (tu := getattr(dtype, \"time_unit\", \"us\")) is not None:\n            tu = cast(\"TimeUnit\", tu)\n            time_unit = tu\n        else:\n            time_unit = \"us\"\n\n        # parse time zone","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/functions/lit.py#L71-L107","documentation":"Raised by polars.lit when the dtype argument is a BaseExtension subclass (an extension-dtype class) instead of an instance of it. Extension dtypes carry parameters (like the extension type they wrap), so polars must receive an instantiated dtype; classes are only accepted for built-in datatypes.","triggerScenarios":"Writing pl.lit(value, dtype=MyExtensionDtype) (class, no parentheses) where MyExtensionDtype subclasses BaseExtension; passing the class when building a literal for a custom extension dtype registered via the plugin system.","commonSituations":"Authoring a polars extension-dtype plugin and mirroring the habitual pl.lit(x, dtype=pl.Int64) style where a class is fine; refactoring code that previously used only built-in dtypes; copy-pasting type annotations (classes) into runtime calls.","solutions":["Instantiate the extension dtype: pl.lit(value, dtype=MyExtensionDType())","If the extension wraps another storage type, you can also call pl.lit(value, dtype=MyExtensionDType().ext_storage()).ext.to(MyExtensionDType()) which is exactly what the instance path does","Check for stray isinstance(dtype, type) in a wrapper/linter if dtype flows in from configuration"],"exampleFix":"// before\npl.lit(value, dtype=MyExtensionDType)\n// after\npl.lit(value, dtype=MyExtensionDType())","handlingStrategy":"type-guard","validationCode":"from polars.datatypes import BaseExtension\n\nif isinstance(dtype, type) and issubclass(dtype, BaseExtension):\n    dtype = dtype()  # instantiate before calling pl.lit","typeGuard":"def is_extension_dtype_instance(d) -> bool:\n    return isinstance(d, BaseExtension)","tryCatchPattern":"try:\n    e = pl.lit(value, dtype=dtype)\nexcept TypeError:\n    e = pl.lit(value, dtype=dtype())  # retry with an instance","preventionTips":["Always instantiate extension dtypes: MyExtDType()","Lint for pl.lit(..., dtype=SomeClass) where SomeClass is not a built-in polars dtype"],"tags":["polars","literal","extension-dtype","plugin-system","type-validation"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}