-
-
Notifications
You must be signed in to change notification settings - Fork 367
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
Closed
Fix/slot hashes #205
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
""" | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.