Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful

This commit is contained in:
Nick Coghlan 2008-07-15 14:27:37 +00:00
parent 9ace15ca25
commit 53663a695e
14 changed files with 134 additions and 134 deletions

View file

@ -48,8 +48,7 @@ class Vector:
def __setitem__(self, i, v):
self.data[i] = v
def __hash__(self):
raise TypeError, "Vectors cannot be hashed"
__hash__ = None # Vectors cannot be hashed
def __nonzero__(self):
raise TypeError, "Vectors cannot be used in Boolean contexts"
@ -85,35 +84,6 @@ class Vector:
raise ValueError, "Cannot compare vectors of different length"
return other
class SimpleOrder(object):
"""
A simple class that defines order but not full comparison.
"""
def __init__(self, value):
self.value = value
def __lt__(self, other):
if not isinstance(other, SimpleOrder):
return True
return self.value < other.value
def __gt__(self, other):
if not isinstance(other, SimpleOrder):
return False
return self.value > other.value
class DumbEqualityWithoutHash(object):
"""
A class that define __eq__, but no __hash__: it shouldn't be hashable.
"""
def __eq__(self, other):
return False
opmap = {
"lt": (lambda a,b: a< b, operator.lt, operator.__lt__),
"le": (lambda a,b: a<=b, operator.le, operator.__le__),
@ -359,39 +329,8 @@ class ListTest(unittest.TestCase):
for op in opmap["lt"]:
self.assertIs(op(x, y), True)
class HashableTest(unittest.TestCase):
"""
Test hashability of classes with rich operators defined.
"""
def test_simpleOrderHashable(self):
"""
A class that only defines __gt__ and/or __lt__ should be hashable.
"""
a = SimpleOrder(1)
b = SimpleOrder(2)
self.assert_(a < b)
self.assert_(b > a)
self.assert_(a.__hash__ is not None)
def test_notHashableException(self):
"""
If a class is not hashable, it should raise a TypeError with an
understandable message.
"""
a = DumbEqualityWithoutHash()
try:
hash(a)
except TypeError, e:
self.assertEquals(str(e),
"unhashable type: 'DumbEqualityWithoutHash'")
else:
raise test_support.TestFailed("Should not be here")
def test_main():
test_support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest, HashableTest)
test_support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest)
if __name__ == "__main__":
test_main()