[Python-checkins] cpython (merge 3.6 -> default): Issue #28735: Fixed the comparison of mock.MagickMock with mock.ANY.

serhiy.storchaka python-checkins at python.org
Sat Jan 21 16:17:52 EST 2017


https://hg.python.org/cpython/rev/597515fcb343
changeset:   106256:597515fcb343
parent:      106253:69bd5c497a82
parent:      106255:4a38781538f7
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Jan 21 23:17:25 2017 +0200
summary:
  Issue #28735: Fixed the comparison of mock.MagickMock with mock.ANY.

files:
  Lib/unittest/mock.py                   |   8 ++++-
  Lib/unittest/test/testmock/testmock.py |  17 +++++++++++--
  Misc/NEWS                              |   2 +
  3 files changed, 22 insertions(+), 5 deletions(-)


diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1765,14 +1765,18 @@
         ret_val = self.__eq__._mock_return_value
         if ret_val is not DEFAULT:
             return ret_val
-        return self is other
+        if self is other:
+            return True
+        return NotImplemented
     return __eq__
 
 def _get_ne(self):
     def __ne__(other):
         if self.__ne__._mock_return_value is not DEFAULT:
             return DEFAULT
-        return self is not other
+        if self is other:
+            return False
+        return NotImplemented
     return __ne__
 
 def _get_iter(self):
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -306,13 +306,24 @@
 
 
     def test_calls_equal_with_any(self):
+        # Check that equality and non-equality is consistent even when
+        # comparing with mock.ANY
+        mm = mock.MagicMock()
+        self.assertTrue(mm == mm)
+        self.assertFalse(mm != mm)
+        self.assertFalse(mm == mock.MagicMock())
+        self.assertTrue(mm != mock.MagicMock())
+        self.assertTrue(mm == mock.ANY)
+        self.assertFalse(mm != mock.ANY)
+        self.assertTrue(mock.ANY == mm)
+        self.assertFalse(mock.ANY != mm)
+
         call1 = mock.call(mock.MagicMock())
         call2 = mock.call(mock.ANY)
-
-        # Check that equality and non-equality is consistent even when
-        # comparing with mock.ANY
         self.assertTrue(call1 == call2)
         self.assertFalse(call1 != call2)
+        self.assertTrue(call2 == call1)
+        self.assertFalse(call2 != call1)
 
 
     def test_assert_called_with(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -215,6 +215,8 @@
 Library
 -------
 
+- Issue #28735: Fixed the comparison of mock.MagickMock with mock.ANY.
+
 - Issue #29197: Removed deprecated function ntpath.splitunc().
 
 - Issue #29210: Removed support of deprecated argument "exclude" in

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list