{"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/68506541d2de983056c9eb244e1ea05fab377dfc/py-polars/src/polars/testing/parametric/strategies/data.py#L370-L406","documentation":"The Hypothesis-based parametric testing strategy for Map columns refuses keys whose dtype is nested (List, Struct, another Map, etc.), because Polars Map keys must be scalar/primitive dtypes. It raises InvalidArgument naming the offending key dtype.","triggerScenarios":"Calling the `maps()` data strategy (directly or via data() / pl.testing parametric strategies) with key= a nested dtype such as pl.List(pl.Int64), pl.Struct(...), or pl.Map(...).","commonSituations":"Auto-generating strategies from a schema where a Map's key dtype was itself nested; writing property-based tests with deliberately exotic dtypes; typos building the Map dtype (swapping key and value arguments so a nested value lands in key).","solutions":["Use a primitive key dtype, e.g. pl.Map(pl.String, pl.Int64) or pl.Map(pl.Int32, ...).","Check the argument order — you may have passed the nested dtype as key when it was meant as value.","If nested keys are semantically required, model the data as List(Struct({key, value})) instead of Map."],"exampleFix":"// before\nst = maps(key=pl.List(pl.Int64), value=pl.Int64)\n// after\nst = maps(key=pl.Int64, value=pl.List(pl.Int64))","handlingStrategy":"validation","validationCode":"import polars as pl\ndef validate_map_key(dtype):\n    key = dtype.key if isinstance(dtype, pl.Map) else dtype\n    if key.is_nested():\n        raise InvalidArgument(f\"map key must be primitive, got {key}\")\n    return key","typeGuard":null,"tryCatchPattern":"try:\n    st = maps(key=key, value=value)\nexcept InvalidArgument as e:\n    if \"nested dtype\" in str(e):\n        st = maps(key=pl.String, value=value)\n    else:\n        raise","preventionTips":["Use only primitive dtypes as Map keys","Double-check key/value argument order when constructing pl.Map","Model nested-key data as List(Struct) instead","Sanitize schemas before generating parametric strategies"],"tags":["python","testing","hypothesis","map-dtype","invalid-argument"],"backgroundTag":"unsupported-dtype","analyzedSha":"68506541d2de983056c9eb244e1ea05fab377dfc","analyzedAt":"2026-09-10T10:08:55.499Z","contentChangedAt":"2026-09-10T10:08:55.499Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}