Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 362f058

Browse files
Issue python#28735: Fixed the comparison of mock.MagickMock with mock.ANY.
1 parent 6ad85bf commit 362f058

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

Lib/unittest/mock.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,14 +1749,18 @@ def __eq__(other):
17491749
ret_val = self.__eq__._mock_return_value
17501750
if ret_val is not DEFAULT:
17511751
return ret_val
1752-
return self is other
1752+
if self is other:
1753+
return True
1754+
return NotImplemented
17531755
return __eq__
17541756

17551757
def _get_ne(self):
17561758
def __ne__(other):
17571759
if self.__ne__._mock_return_value is not DEFAULT:
17581760
return DEFAULT
1759-
return self is not other
1761+
if self is other:
1762+
return False
1763+
return NotImplemented
17601764
return __ne__
17611765

17621766
def _get_iter(self):

Lib/unittest/test/testmock/testmock.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,24 @@ def test_call_args_comparison(self):
306306

307307

308308
def test_calls_equal_with_any(self):
309-
call1 = mock.call(mock.MagicMock())
310-
call2 = mock.call(mock.ANY)
311-
312309
# Check that equality and non-equality is consistent even when
313310
# comparing with mock.ANY
311+
mm = mock.MagicMock()
312+
self.assertTrue(mm == mm)
313+
self.assertFalse(mm != mm)
314+
self.assertFalse(mm == mock.MagicMock())
315+
self.assertTrue(mm != mock.MagicMock())
316+
self.assertTrue(mm == mock.ANY)
317+
self.assertFalse(mm != mock.ANY)
318+
self.assertTrue(mock.ANY == mm)
319+
self.assertFalse(mock.ANY != mm)
320+
321+
call1 = mock.call(mock.MagicMock())
322+
call2 = mock.call(mock.ANY)
314323
self.assertTrue(call1 == call2)
315324
self.assertFalse(call1 != call2)
325+
self.assertTrue(call2 == call1)
326+
self.assertFalse(call2 != call1)
316327

317328

318329
def test_assert_called_with(self):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issue #28735: Fixed the comparison of mock.MagickMock with mock.ANY.
17+
1618
- Issue #29011: Fix an important omission by adding Deque to the typing module.
1719

1820
- Issue #29219: Fixed infinite recursion in the repr of uninitialized

0 commit comments

Comments
 (0)