{"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":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/lit.py","lineNumber":97,"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    elif dtype == Map:\n        value_s = pl.Series(\"literal\", [value], dtype=dtype)\n        return wrap_expr(plr.lit(value_s._s, allow_object, is_scalar=True))\n    elif dtype == Unknown:\n        dtype = None\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)","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/functions/lit.py#L79-L115","documentation":"`pl.lit(dtype=...)` was given the extension dtype CLASS (e.g. a BaseExtension subclass) instead of an INSTANCE of it. Polars requires a concrete instance to know storage/extension metadata, so it refuses to construct a literal from the bare class.","triggerScenarios":"Calling `pl.lit(value, dtype=SomeExtensionDtype)` where `SomeExtensionDtype` is the class itself rather than `SomeExtensionDtype(...)`; commonly after an API change where extension dtypes switched from class-level to instance-level usage.","commonSituations":"Passing a dtype variable that holds the class from a factory/registry; forgetting parentheses on a parameterized extension dtype; migrating code from older polars versions that accepted classes.","solutions":["Instantiate the extension dtype, e.g. pass `SomeExtensionDtype(...)` instead of `SomeExtensionDtype`","If the dtype comes from config/registry, call it (with required args) before passing to pl.lit","Check `isinstance(dtype, type)` at your call site and instantiate before calling pl.lit"],"exampleFix":"// before\npl.lit(1, dtype=pl.Categorical)\n// after (for a custom extension dtype)\npl.lit(1, dtype=MyExtensionDtype())","handlingStrategy":"type-guard","validationCode":"if isinstance(dtype, type):\n    dtype = dtype()  # instantiate extension dtype class\npl.lit(value, dtype=dtype)","typeGuard":"def is_dtype_instance(dtype) -> bool:\n    return not isinstance(dtype, type)","tryCatchPattern":"try:\n    expr = pl.lit(value, dtype=dtype)\nexcept TypeError as e:\n    if 'should be an instance' in str(e):\n        expr = pl.lit(value, dtype=dtype())\n    else:\n        raise","preventionTips":["Always instantiate parameterized dtypes before passing them","Wrap dtype construction in a helper that accepts class or instance"],"tags":["python","polars","dtype","type-error"],"backgroundTag":"unsupported-dtype","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"}