pola-rs/polars · error · TypeError

DataTypeGroup items must be dtypes; found {qualified_type_na

Error message

DataTypeGroup items must be dtypes; found {qualified_type_name(it)!r}

What it means

DataTypeGroup — the frozenset subclass behind INTEGER_DTYPES, FLOAT_DTYPES, etc. — validates in __new__ that every item is a DataType or DataTypeClass instance. Anything else (strings like 'i64', Python types, arbitrary objects) raises TypeError with the offending item's qualified type name.

Source

Thrown at py-polars/src/polars/datatypes/group.py:70

    def __new__(
        cls, items: Iterable[DataType | DataTypeClass], *, match_base_type: bool = True
    ) -> Self:
        """
        Construct a DataTypeGroup.

        Parameters
        ----------
        items :
            iterable of data types
        match_base_type:
            match the base type
        """
        for it in items:
            if not isinstance(it, (DataType, DataTypeClass)):
                from polars._utils.various import qualified_type_name

                msg = f"DataTypeGroup items must be dtypes; found {qualified_type_name(it)!r}"
                raise TypeError(msg)

        dtype_group = super().__new__(cls, items)
        dtype_group._match_base_type = match_base_type
        return dtype_group

    def __contains__(self, item: Any) -> bool:
        if self._match_base_type and isinstance(item, (DataType, DataTypeClass)):
            item = item.base_type()
        return super().__contains__(item)


SIGNED_INTEGER_DTYPES: Final[frozenset[PolarsIntegerType]] = DataTypeGroup(
    [
        Int8,
        Int16,
        Int32,
        Int64,
        Int128,

View on GitHub (pinned to df599052da)

Solutions

  1. Pass dtype classes or instances: pl.Int64, pl.Float64, pl.Int64()
  2. Resolve strings to dtypes before building the group, e.g. via a name-to-dtype mapping built from polars.datatypes
  3. Validate config-supplied items against polars datatypes at load time

Example fix

# before
group = DataTypeGroup(['i64', 'f64'])  # TypeError

# after
group = DataTypeGroup([pl.Int64, pl.Float64])
Defensive patterns

Strategy: type-guard

Validate before calling

from polars.datatypes import DataType, DataTypeClass, DataTypeGroup

items = ['i64', 'f64']  # e.g. from config
assert all(isinstance(it, (DataType, DataTypeClass)) for it in items), 'dtype group received non-dtype items'
group = DataTypeGroup(items)

Type guard

from polars.datatypes import DataType, DataTypeClass

def all_items_are_dtypes(items) -> bool:
    return all(isinstance(it, (DataType, DataTypeClass)) for it in items)

Prevention

When it happens

Trigger: Constructing DataTypeGroup with non-dtype items: DataTypeGroup(['i64', 'f64']), DataTypeGroup([int, float]), or building a group from config/YAML-supplied dtype names that were never resolved to actual dtypes.

Common situations: Parsing dtype names from configuration files into dtype groups; refactors that replaced dtype classes with their string reprs; copy-pasting short dtype codes ('i64') from polars display output into code.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/e9da0e5fcddab05e. Report an issue: GitHub.