{"record":{"id":"c2c5fbcc8f19fa5b","repo":"pola-rs/polars","slug":"should-be-hashable","errorCode":null,"errorMessage":"should be hashable","messagePattern":"should be hashable","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-python/src/conversion/mod.rs","lineNumber":811,"sourceCode":"}\n\n#[derive(Debug)]\n#[repr(transparent)]\npub struct ObjectValue {\n    pub inner: Py<PyAny>,\n}\n\nimpl Clone for ObjectValue {\n    fn clone(&self) -> Self {\n        Python::attach(|py| Self {\n            inner: self.inner.clone_ref(py),\n        })\n    }\n}\n\nimpl Hash for ObjectValue {\n    fn hash<H: Hasher>(&self, state: &mut H) {\n        let h = Python::attach(|py| self.inner.bind(py).hash().expect(\"should be hashable\"));\n        state.write_isize(h)\n    }\n}\n\nimpl Eq for ObjectValue {}\n\nimpl PartialEq for ObjectValue {\n    fn eq(&self, other: &Self) -> bool {\n        Python::attach(|py| {\n            match self\n                .inner\n                .bind(py)\n                .rich_compare(other.inner.bind(py), CompareOp::Eq)\n            {\n                Ok(result) => result.is_truthy().unwrap(),\n                Err(_) => false,\n            }\n        })","sourceCodeStart":793,"sourceCodeEnd":829,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-python/src/conversion/mod.rs#L793-L829","documentation":"The Hash impl for ObjectValue (wrapping an arbitrary Python object) in crates/polars-python/src/conversion/mod.rs calls the Python object's __hash__ and expects it to succeed. If the stored Python object is unhashable (e.g. list, dict, set), the `expect(\"should be hashable\")` panics, raised from `hash` (called by `tot_hash`).","triggerScenarios":"Putting unhashable Python objects into an Object-dtype Series/DataFrame and then triggering Rust-side hashing — e.g. using the Series in a unique/group_by/join/hash operation or hash-based dedup.","commonSituations":"Storing lists/dicts as 'object' column values and then calling .unique(), .n_unique(), group_by on that column, or a join involving the object column.","solutions":["Only store hashable Python objects (or make the object hashable by defining __hash__ and __eq__)","Avoid hashing operations on object columns; convert values to hashable representations (tuples instead of lists, immutable structs)","Restructure the data into supported dtypes (Struct/List) instead of raw Python objects"],"exampleFix":"# before\npl.Series(\"cfg\", [{\"a\": 1}, {\"a\": 2}]).n_unique()  # dicts unhashable -> panic\n# after\npl.Series(\"cfg\", [((\"a\", 1),), ((\"a\", 2),)]).n_unique()","handlingStrategy":"validation","validationCode":"if df.schema[\"col\"] == pl.Object:\n    assert all(isinstance(v, collections.abc.Hashable) for v in df[\"col\"].to_list()), \"object column contains unhashable values; unique/group_by will panic\"","typeGuard":"import collections.abc\ndef all_hashable(values: list) -> bool:\n    return all(isinstance(v, collections.abc.Hashable) for v in values)","tryCatchPattern":"try:\n    n = s.n_unique()\nexcept pl.exceptions.PanicException as e:\n    if \"should be hashable\" in str(e):\n        n = len({repr(v) for v in s.to_list()})  # fallback on representation\n    else:\n        raise","preventionTips":["Only put hashable objects in Object-dtype columns if you plan to use unique/group_by/join","Implement __hash__ and __eq__ on custom classes stored in object columns","Prefer native dtypes (Struct/List) over raw Python objects"],"tags":["python","object-dtype","hashable","panic"],"backgroundTag":"unsupported-operation","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"}