{"record":{"id":"8572dffa8dba14b5","repo":"pola-rs/polars","slug":"map-keys-cannot-be-a-nested-dtype-got-key-r","errorCode":null,"errorMessage":"map keys cannot be a nested dtype, got {key!r}","messagePattern":"map keys cannot be a nested dtype, got (.+?)","errorType":"validation","errorClass":"InvalidArgument","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/testing/parametric/strategies/data.py","lineNumber":388,"sourceCode":"    Parameters\n    ----------\n    key\n        The data type of the keys. Cannot be nested, since keys become Python dict keys\n        and those must be hashable.\n    value\n        The data type of the values.\n    min_size\n        The minimum number of entries in a map.\n    max_size\n        The maximum number of entries in a map.\n    allow_null\n        Allow nulls as possible values.\n    **kwargs\n        Additional arguments that are passed to nested data generation strategies.\n    \"\"\"\n    if key.is_nested():\n        msg = f\"map keys cannot be a nested dtype, got {key!r}\"\n        raise InvalidArgument(msg)\n    if key == Null:\n        # Reached via a bare `pl.Map`, which cannot say what its keys are. Generating\n        # `{None: None}` would be worse than refusing: Map keys are never null.\n        msg = \"map keys cannot be Null; specify a key type, e.g. `pl.Map(pl.String, pl.Int64)`\"\n        raise InvalidArgument(msg)\n    if max_size is None:\n        max_size = _DEFAULT_MAP_SIZE_LIMIT\n\n    inner_kwargs = {\n        k: v for k, v in kwargs.items() if k not in (\"min_size\", \"max_size\")\n    }\n    return st.dictionaries(\n        data(key, allow_null=False, **inner_kwargs),\n        data(value, allow_null=allow_null, **inner_kwargs),\n        min_size=min_size,\n        max_size=max_size,\n    )\n","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/testing/parametric/strategies/data.py#L370-L406","documentation":"The maps() strategy in polars' parametric testing module refuses Map dtypes whose key dtype is nested (e.g. List, Struct). Polars Map keys must be flat scalar types, so generating data with nested keys is impossible and InvalidArgument is raised up front.","triggerScenarios":"Calling maps(key=pl.List(pl.Int64), value=...) or passing any nested key dtype; also triggering via data(pl.Map(pl.List(...), value)) with a nested key.","commonSituations":"Modeling JSON-like structures where dict keys are arrays; translating a schema from another system that permits complex keys; typos nesting the key dtype when constructing pl.Map.","solutions":["Use a flat key dtype such as pl.String or pl.Int64 for the Map key.","If keys must be complex, serialize them to strings (e.g. JSON-encode) and store as a String-keyed Map.","Check dtype.is_nested() before calling maps() and fail fast with a clearer message."],"exampleFix":"// before\nmaps(key=pl.List(pl.Int64), value=pl.String)\n// after\nmaps(key=pl.String, value=pl.String)","handlingStrategy":"validation","validationCode":"if key.is_nested():\n    raise ValueError(f\"Map key must be flat, got {key}\")","typeGuard":"def is_flat_key(dtype) -> bool:\n    return not dtype.is_nested() and dtype != pl.Null","tryCatchPattern":"from polars.exceptions import InvalidArgument\ntry:\n    strat = maps(key=key_dtype, value=value_dtype)\nexcept InvalidArgument:\n    strat = maps(key=pl.String, value=value_dtype)  # serialize keys instead","preventionTips":["Always declare Map dtypes with explicit flat key types.","Serialize complex keys (JSON-encode) before storing in a Map.","Run a schema lint that rejects nested Map keys in test schemas."],"tags":["polars","hypothesis","map","dtype","testing"],"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"}