{"record":{"id":"2e68363a612519f4","repo":"pola-rs/polars","slug":"map-keys-cannot-be-null-specify-a-key-type-e-g","errorCode":null,"errorMessage":"map keys cannot be Null; specify a key type, e.g. `pl.Map(pl.String, pl.Int64)`","messagePattern":"map keys cannot be Null; specify a key type, e\\.g\\. `pl\\.Map\\(pl\\.String, pl\\.Int64\\)`","errorType":"validation","errorClass":"InvalidArgument","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/testing/parametric/strategies/data.py","lineNumber":393,"sourceCode":"    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\n\ndef nulls() -> SearchStrategy[None]:\n    \"\"\"Create a strategy for generating null values.\"\"\"\n    return st.none()\n","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/testing/parametric/strategies/data.py#L375-L411","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Specify a concrete key type: pl.Map(pl.String, pl.Int64) instead of a bare pl.Map.","Derive the key dtype from the actual data or schema instead of relying on defaults.","Guard with `if key == Null: ...` before generating to give a better error in your test harness."],"exampleFix":"// before\npl.Map\n// after\npl.Map(pl.String, pl.Int64)","handlingStrategy":"validation","validationCode":"if key == pl.Null:\n    raise ValueError(\"specify a Map key type, e.g. pl.Map(pl.String, pl.Int64)\")","typeGuard":"def has_concrete_key(map_dtype: pl.DataType) -> bool:\n    return isinstance(map_dtype, pl.Map) and map_dtype.key != 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)","preventionTips":["Never write a bare pl.Map in schemas; always specify key and value types.","Add a schema check that fails fast on Null map keys.","Default missing key types to pl.String rather than Null."],"tags":["polars","hypothesis","map","null","testing"],"backgroundTag":"null-argument","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"}