{"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/68506541d2de983056c9eb244e1ea05fab377dfc/py-polars/src/polars/testing/parametric/strategies/data.py#L375-L411","documentation":"The parametric `maps()` strategy also refuses Null as the Map key dtype. A bare pl.Map carries no key type; rather than generating useless {None: None} data, the strategy raises InvalidArgument telling you to specify a concrete key type.","triggerScenarios":"Using data()/maps() for a column typed bare pl.Map (no key/value parameters), or explicitly passing key=pl.Null to maps().","commonSituations":"Property-based tests driven by a schema that declares `pl.Map` without parameters; dtype round-trip tests where parameterized Map info was lost during serialization.","solutions":["Specify key and value types in the dtype: pl.Map(pl.String, pl.Int64), then pass that to the strategy.","If the dtype comes from an external schema, fill in the Map parameters before generating strategies.","Choose a stable key dtype matching your real data (String, Int64, etc.)."],"exampleFix":"// before\nst = data(dtype=pl.Map)\n// after\nst = data(dtype=pl.Map(pl.String, pl.Int64))","handlingStrategy":"validation","validationCode":"import polars as pl\ndef require_parameterized_map(dtype):\n    if isinstance(dtype, pl.Map):\n        key = dtype.key\n    else:\n        key = None\n    if key is None or key == pl.Null:\n        raise InvalidArgument(\"specify key/value types, e.g. pl.Map(pl.String, pl.Int64)\")\n    return dtype","typeGuard":null,"tryCatchPattern":"try:\n    st = data(dtype=dtype)\nexcept InvalidArgument as e:\n    if \"map keys cannot be Null\" in str(e):\n        st = data(dtype=pl.Map(pl.String, pl.Int64))\n    else:\n        raise","preventionTips":["Never declare bare pl.Map in schemas; always parameterize","Fill in Map parameters when importing dtypes from external schema formats","Choose a concrete, stable key dtype matching production data"],"tags":["python","testing","hypothesis","map-dtype","invalid-argument"],"backgroundTag":"invalid-argument-value","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"}