Skip to content

Commit

Permalink
Fixed frozen and cache_hash incompatibility
Browse files Browse the repository at this point in the history
Fixes GH issue #611.
  • Loading branch information
pganssle committed Jan 8, 2020
1 parent b6bd8c8 commit a2a6713
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.d/611.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed serialization of classes with ``frozen=True`` and ``cache_hash=True``, which has been broken since 19.1.0.
14 changes: 11 additions & 3 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,17 @@ def _patch_original_class(self):
"See https://github.com/python-attrs/attrs/issues/494 ."
)

def cache_hash_set_state(chss_self, _):
# clear hash code cache
setattr(chss_self, _hash_cache_field, None)
# Clears the cached hash state on serialization; for frozen
# classes we need to bypass the class's setattr method.
if self._frozen:

def cache_hash_set_state(chss_self, _):
object.__setattr__(chss_self, _hash_cache_field, None)

else:

def cache_hash_set_state(chss_self, _):
setattr(chss_self, _hash_cache_field, None)

cls.__setstate__ = cache_hash_set_state

Expand Down
11 changes: 11 additions & 0 deletions tests/test_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,17 @@ class C2(C):

assert [C2] == C.__subclasses__()

def test_cache_hash_with_frozen_serializes(self):
"""
Frozen classes with cache_hash should be serializable.
"""

@attr.s(cache_hash=True, frozen=True)
class C(object):
pass

copy.deepcopy(C)


class TestMakeOrder:
"""
Expand Down

0 comments on commit a2a6713

Please sign in to comment.