From 106d47f4d3e8ea1e114a11f2bec1b1c7eb93d13d Mon Sep 17 00:00:00 2001 From: Matthias Diener Date: Wed, 6 Dec 2023 17:27:50 -0600 Subject: [PATCH] Do not store cached hash value when pickling --- immutabledict/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/immutabledict/__init__.py b/immutabledict/__init__.py index 152ce4e..0d04ae5 100644 --- a/immutabledict/__init__.py +++ b/immutabledict/__init__.py @@ -45,6 +45,11 @@ def __new__(cls, *args: Any, **kwargs: Any) -> immutabledict[_K, _V]: # noqa: D setattr(inst, "_hash", None) return inst + def __reduce__(self) -> Tuple[Any, ...]: + # Do not stored the cached hash value when pickling + # as the value might change across Python invocations. + return (self.__class__, (self._dict,)) + def __getitem__(self, key: _K) -> _V: return self._dict[key]