Skip to content
Merged
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 Lib/_collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ def _hash(self):
hx = hash(x)
h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
h &= MASK
h ^= (h >> 11) ^ (h >> 25)
h = h * 69069 + 907133923
h &= MASK
if h > MAX:
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,18 @@ def __repr__(self):
self.assertTrue(f1 != l1)
self.assertTrue(f1 != l2)

def test_Set_hash_matches_frozenset(self):
sets = [
{}, {1}, {None}, {-1}, {0.0}, {"abc"}, {1, 2, 3},
{10**100, 10**101}, {"a", "b", "ab", ""}, {False, True},
{object(), object(), object()}, {float("nan")}, {frozenset()},
{*range(1000)}, {*range(1000)} - {100, 200, 300},
{*range(sys.maxsize - 10, sys.maxsize + 10)},
]
for s in sets:
fs = frozenset(s)
self.assertEqual(hash(fs), Set._hash(fs), msg=s)

def test_Mapping(self):
for sample in [dict]:
self.assertIsInstance(sample(), Mapping)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The implementation of ``collections.abc.Set._hash()`` now matches that of ``frozenset.__hash__()``.