pola-rs/polars · error · InvalidArgument

map keys cannot be Null; specify a key type, e.g…

Error message

map keys cannot be Null; specify a key type, e.g. `pl.Map(pl.String, pl.Int64)`

What it means

maps() raises this when the Map key dtype is pl.Null. A bare pl.Map() carries no key type, so it defaults to Null; the strategy refuses to generate such data because Map keys are never null, and instead asks the caller to specify a concrete key type.

Solutions

  1. Specify a concrete key type: pl.Map(pl.String, pl.Int64) instead of a bare pl.Map.
  2. Derive the key dtype from the actual data or schema instead of relying on defaults.
  3. Guard with `if key == Null: ...` before generating to give a better error in your test harness.

Example fix

// before
pl.Map
// after
pl.Map(pl.String, pl.Int64)
Defensive patterns

Strategy: validation

Validate before calling

if key == pl.Null:
    raise ValueError("specify a Map key type, e.g. pl.Map(pl.String, pl.Int64)")

Type guard

def has_concrete_key(map_dtype: pl.DataType) -> bool:
    return isinstance(map_dtype, pl.Map) and map_dtype.key != pl.Null

Try / catch

from polars.exceptions import InvalidArgument
try:
    strat = maps(key=key_dtype, value=value_dtype)
except InvalidArgument:
    strat = maps(key=pl.String, value=value_dtype)

Prevention

When it happens

Trigger: Writing pl.Map without arguments (e.g. pl.Map or pl.Map(...)) in a schema passed to parametric testing, causing key to be Null; explicitly passing key=pl.Null.

Common situations: Declaring a bare pl.Map column in a test schema; dtype instantiation filling in Null for unspecified key types; copying a schema from a frame where the map key type was never pinned down.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/2e68363a612519f4. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/testing/parametric/strategies/data.py:393

    value
        The data type of the values.
    min_size
        The minimum number of entries in a map.
    max_size
        The maximum number of entries in a map.
    allow_null
        Allow nulls as possible values.
    **kwargs
        Additional arguments that are passed to nested data generation strategies.
    """
    if key.is_nested():
        msg = f"map keys cannot be a nested dtype, got {key!r}"
        raise InvalidArgument(msg)
    if key == Null:
        # Reached via a bare `pl.Map`, which cannot say what its keys are. Generating
        # `{None: None}` would be worse than refusing: Map keys are never null.
        msg = "map keys cannot be Null; specify a key type, e.g. `pl.Map(pl.String, pl.Int64)`"
        raise InvalidArgument(msg)
    if max_size is None:
        max_size = _DEFAULT_MAP_SIZE_LIMIT

    inner_kwargs = {
        k: v for k, v in kwargs.items() if k not in ("min_size", "max_size")
    }
    return st.dictionaries(
        data(key, allow_null=False, **inner_kwargs),
        data(value, allow_null=allow_null, **inner_kwargs),
        min_size=min_size,
        max_size=max_size,
    )


def nulls() -> SearchStrategy[None]:
    """Create a strategy for generating null values."""
    return st.none()

View on GitHub (pinned to fe841f959e)