Skip to content

Commit

Permalink
Merge pull request #287 from matthiasdiener/pickle-reduce
Browse files Browse the repository at this point in the history
Do not store cached hash value when pickling
  • Loading branch information
corenting authored Dec 16, 2023
2 parents c9504d0 + f3ca52d commit 1d9225d
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions immutabledict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
KeysView,
Mapping,
Optional,
Tuple,
Type,
TypeVar,
ValuesView,
Expand Down Expand Up @@ -45,6 +46,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 store 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]

Expand Down

0 comments on commit 1d9225d

Please sign in to comment.