[Python-checkins] Fix DeprecationWarning in tests (#4345)

Victor Stinner webhook-mailer at python.org
Wed Nov 8 17:45:58 EST 2017


https://github.com/python/cpython/commit/2ce1ef54d30606e61e85526861673bac3faef617
commit: 2ce1ef54d30606e61e85526861673bac3faef617
branch: 2.7
author: Victor Stinner <victor.stinner at gmail.com>
committer: GitHub <noreply at github.com>
date: 2017-11-08T14:45:55-08:00
summary:

Fix DeprecationWarning in tests (#4345)

Define __hash__() in test_functools and test_itertools to fix the
following warning:

DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x

files:
M Lib/test/test_functools.py
M Lib/test/test_itertools.py

diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index b3372ea7c33..2847573035f 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -609,6 +609,8 @@ def __gt__(self, other):
                 return self.value > other.value
             def __eq__(self, other):
                 return self.value == other.value
+            def __hash__(self):
+                return hash(self.value)
         self.assertTrue(A(1) != A(2))
         self.assertFalse(A(1) != A(1))
 
@@ -620,6 +622,8 @@ def __gt__(self, other):
                 return self.value > other.value
             def __eq__(self, other):
                 return self.value == other.value
+            def __hash__(self):
+                return hash(self.value)
         self.assertTrue(A(1) != A(2))
         self.assertFalse(A(1) != A(1))
 
@@ -633,6 +637,8 @@ def __eq__(self, other):
                 return self.value == other.value
             def __ne__(self, other):
                 raise RuntimeError(self, other)
+            def __hash__(self):
+                return hash(self.value)
         with self.assertRaises(RuntimeError):
             A(1) != A(2)
         with self.assertRaises(RuntimeError):
@@ -648,6 +654,8 @@ def __eq__(self, other):
                 return self.value == other.value
             def __ne__(self, other):
                 raise RuntimeError(self, other)
+            def __hash__(self):
+                return hash(self.value)
         with self.assertRaises(RuntimeError):
             A(1) != A(2)
         with self.assertRaises(RuntimeError):
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 7eee81a568c..1e6db3426e4 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -1491,6 +1491,8 @@ def __eq__(self, other):
                 if K.i == 1:
                     next(g, None)
                 return True
+            def __hash__(self):
+                return 1
         g = next(groupby(range(10), K))[1]
         for j in range(2):
             next(g, None)  # shouldn't crash



More information about the Python-checkins mailing list