diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2008-07-15 14:27:37 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2008-07-15 14:27:37 (GMT) |
commit | 53663a695ef2bb96ac0252cd4cc4aa40d4f953be (patch) | |
tree | e241ef71b353f8b3162179b1eed5a3f4eaae0c5f /Lib/test | |
parent | 9ace15ca25e1e72e1b943190a5f4efbd7d118de3 (diff) | |
download | cpython-53663a695ef2bb96ac0252cd4cc4aa40d4f953be.zip cpython-53663a695ef2bb96ac0252cd4cc4aa40d4f953be.tar.gz cpython-53663a695ef2bb96ac0252cd4cc4aa40d4f953be.tar.bz2 |
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/seq_tests.py | 3 | ||||
-rw-r--r-- | Lib/test/test_descr.py | 8 | ||||
-rw-r--r-- | Lib/test/test_hash.py | 79 | ||||
-rw-r--r-- | Lib/test/test_richcmp.py | 65 |
4 files changed, 89 insertions, 66 deletions
diff --git a/Lib/test/seq_tests.py b/Lib/test/seq_tests.py index d9b2d29..c273a3f 100644 --- a/Lib/test/seq_tests.py +++ b/Lib/test/seq_tests.py @@ -214,8 +214,7 @@ class CommonTest(unittest.TestCase): # So instances of AllEq must be found in all non-empty sequences. def __eq__(self, other): return True - def __hash__(self): - raise NotImplemented + __hash__ = None # Can't meet hash invariant requirements self.assert_(AllEq() not in self.type2test([])) self.assert_(AllEq() in self.type2test([1])) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 3c607f7..53b7611 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -3283,12 +3283,20 @@ order (MRO) for bases """ self.assertEqual(hash(d), 144) D.__hash__ = lambda self: 100 self.assertEqual(hash(d), 100) + D.__hash__ = None + self.assertRaises(TypeError, hash, d) del D.__hash__ self.assertEqual(hash(d), 144) + B.__hash__ = None + self.assertRaises(TypeError, hash, d) del B.__hash__ self.assertEqual(hash(d), 314) + C.__hash__ = None + self.assertRaises(TypeError, hash, d) del C.__hash__ self.assertEqual(hash(d), 42) + A.__hash__ = None + self.assertRaises(TypeError, hash, d) del A.__hash__ self.assertEqual(hash(d), orig_hash) d.foo = 42 diff --git a/Lib/test/test_hash.py b/Lib/test/test_hash.py index 07b65da..f3954c2 100644 --- a/Lib/test/test_hash.py +++ b/Lib/test/test_hash.py @@ -1,9 +1,11 @@ # test the invariant that # iff a==b then hash(a)==hash(b) # +# Also test that hash implementations are inherited as expected import unittest from test import test_support +from collections import Hashable class HashEqualityTestCase(unittest.TestCase): @@ -39,8 +41,83 @@ class HashEqualityTestCase(unittest.TestCase): self.same_hash(float(0.5), complex(0.5, 0.0)) +_default_hash = object.__hash__ +class DefaultHash(object): pass + +_FIXED_HASH_VALUE = 42 +class FixedHash(object): + def __hash__(self): + return _FIXED_HASH_VALUE + +class OnlyEquality(object): + def __eq__(self, other): + return self is other + +class OnlyInequality(object): + def __ne__(self, other): + return self is not other + +class OnlyCmp(object): + def __cmp__(self, other): + return cmp(id(self), id(other)) + +class InheritedHashWithEquality(FixedHash, OnlyEquality): pass +class InheritedHashWithInequality(FixedHash, OnlyInequality): pass +class InheritedHashWithCmp(FixedHash, OnlyCmp): pass + +class NoHash(object): + __hash__ = None + +class HashInheritanceTestCase(unittest.TestCase): + default_expected = [object(), + DefaultHash(), + ] + fixed_expected = [FixedHash(), + InheritedHashWithEquality(), + InheritedHashWithInequality(), + InheritedHashWithCmp(), + ] + # TODO: Change these to expecting an exception + # when forward porting to Py3k + warning_expected = [OnlyEquality(), + OnlyInequality(), + OnlyCmp(), + ] + error_expected = [NoHash()] + + def test_default_hash(self): + for obj in self.default_expected: + self.assertEqual(hash(obj), _default_hash(obj)) + + def test_fixed_hash(self): + for obj in self.fixed_expected: + self.assertEqual(hash(obj), _FIXED_HASH_VALUE) + + def test_warning_hash(self): + for obj in self.warning_expected: + # TODO: Check for the expected Py3k warning here + obj_hash = hash(obj) + self.assertEqual(obj_hash, _default_hash(obj)) + + def test_error_hash(self): + for obj in self.error_expected: + self.assertRaises(TypeError, hash, obj) + + def test_hashable(self): + objects = (self.default_expected + + self.fixed_expected + + self.warning_expected) + for obj in objects: + self.assert_(isinstance(obj, Hashable), repr(obj)) + + def test_not_hashable(self): + for obj in self.error_expected: + self.assertFalse(isinstance(obj, Hashable), repr(obj)) + + def test_main(): - test_support.run_unittest(HashEqualityTestCase) + test_support.run_unittest(HashEqualityTestCase, + HashInheritanceTestCase) if __name__ == "__main__": diff --git a/Lib/test/test_richcmp.py b/Lib/test/test_richcmp.py index db6d31f..ad68386 100644 --- a/Lib/test/test_richcmp.py +++ b/Lib/test/test_richcmp.py @@ -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() |