Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/slot hashes #205

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Changes:
`#198 <https:/python-attrs/attrs/issues/198>`_
- ``attr.Factory`` is now hashable again.
`#204 <https:/python-attrs/attrs/issues/204>`_
- Hashing behavior when ``hash=False`` is now consistent for slot and non-slot classes.


----
Expand Down
10 changes: 10 additions & 0 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ def wrap(cls):
cls = _add_pickle(cls)
if slots is True:
cls_dict = dict(cls.__dict__)
was_unhashable = ('__hash__' in cls_dict and

This comment was marked as spam.

cls_dict['__hash__'] is None)
cls_dict["__slots__"] = tuple(ca_list)
for ca_name in ca_list:
# It might not actually be in there, e.g. if using 'these'.
Expand All @@ -376,6 +378,14 @@ def wrap(cls):

qualname = getattr(cls, "__qualname__", None)
cls = type(cls)(cls.__name__, cls.__bases__, cls_dict)

if (not was_unhashable and hash is False and

This comment was marked as spam.

'__hash__' in cls.__dict__ and
cls.__dict__['__hash__'] is None):

# Python just set __hash__ = None for us, revert this.
del cls.__hash__

if qualname is not None:
cls.__qualname__ = qualname

Expand Down
105 changes: 105 additions & 0 deletions tests/test_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"""
Tests for our hashing functionality.
"""
import pytest

import attr

from hypothesis import given
from hypothesis.strategies import booleans


@given(slots=booleans(), frozen=booleans(), cmp=booleans())
def test_no_subclass_no_hash(slots, frozen, cmp):

This comment was marked as spam.

This comment was marked as spam.

"""
Classes inheriting from ``object``, and setting ``hash=False`` fall back
to ``object.__hash__``.
"""
@attr.s(hash=False, slots=slots, frozen=frozen, cmp=cmp)
class A(object):
a = attr.ib()

assert A.__hash__ == object.__hash__
hash(A(1)) # Should not raise.

assert hash(A(1)) != hash(A(1)) # Identity-based hash.


@given(slots=booleans(), frozen=booleans(), cmp=booleans())
def test_no_subclass_has_hash(slots, frozen, cmp):
"""
Classes inheriting from ``object``, and setting ``hash=True`` use the
``attrs``-generated hash.
"""
@attr.s(hash=True, slots=slots, frozen=frozen, cmp=cmp)
class A(object):
a = attr.ib()

assert A.__hash__ is not object.__hash__
hash(A(1)) # Should not raise.

assert hash(A(1)) == hash(A(1))


@given(slots=booleans(), frozen=booleans(), cmp=booleans())
def test_no_subclass_default_hash(slots, frozen, cmp):
"""
Classes inheriting from ``object``, and setting ``hash=None`` set their
``__hash__`` according to whether they're frozen and comparable.
"""
@attr.s(slots=slots, frozen=frozen, cmp=cmp)
class A(object):
a = attr.ib()

if frozen and cmp:
assert A.__hash__ is not object.__hash__
hash(A(1)) # Should not raise.

assert hash(A(1)) == hash(A(1))
elif cmp and not frozen:
# We make the class unhashable.
assert A.__hash__ is None

with pytest.raises(TypeError):
hash(A(1))
elif not cmp:
# Fall back to object.__hash__.
assert A.__hash__ == object.__hash__
hash(A(1)) # Should not raise.

assert hash(A(1)) != hash(A(1)) # Identity-based hash.


@given(super_slots=booleans(), super_frozen=booleans(), slots=booleans(),
frozen=booleans())
def test_subclass_default_hash(super_slots, super_frozen, slots, frozen):
"""
``cmp=False`` and ``hash=None`` makes classes use the ``__hash__`` of their
superclass.
"""
@attr.s(slots=super_slots, frozen=super_frozen)
class A(object):
a = attr.ib()

@attr.s(slots=slots, frozen=frozen, cmp=False)
class B(A):
b = attr.ib()

assert B.__hash__ == A.__hash__ # ``is`` doesn't work on Py2.


@given(slots=booleans(), frozen=booleans())
def test_make_unhashable(slots, frozen):
"""
Creating classes that have a __cmp__ but are unhashable is possible.
"""
@attr.s(cmp=True, hash=False, slots=slots, frozen=frozen)
class A(object):
a = attr.ib()

__hash__ = None

with pytest.raises(TypeError):
hash(A(1))

assert A.__hash__ is None