Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce91387

Browse files
committed
Issue #25195: Fix a regression in mock.MagicMock
_Call is a subclass of tuple (changeset 3603bae63c13 only works for classes) so we need to implement __ne__ ourselves. Patch by Andrew Plummer.
1 parent fa0f62d commit ce91387

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

Lib/unittest/mock.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,9 @@ def __eq__(self, other):
20292029
return (other_args, other_kwargs) == (self_args, self_kwargs)
20302030

20312031

2032+
__ne__ = object.__ne__
2033+
2034+
20322035
def __call__(self, *args, **kwargs):
20332036
if self.name is None:
20342037
return _Call(('', args, kwargs), name='()')

Lib/unittest/test/testmock/testmock.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ def test_call_args_comparison(self):
304304
# an exception. See issue 24857.
305305
self.assertFalse(mock.call_args == "a long sequence")
306306

307+
308+
def test_calls_equal_with_any(self):
309+
call1 = mock.call(mock.MagicMock())
310+
call2 = mock.call(mock.ANY)
311+
312+
# Check that equality and non-equality is consistent even when
313+
# comparing with mock.ANY
314+
self.assertTrue(call1 == call2)
315+
self.assertFalse(call1 != call2)
316+
317+
307318
def test_assert_called_with(self):
308319
mock = Mock()
309320
mock()
@@ -319,6 +330,12 @@ def test_assert_called_with(self):
319330
mock.assert_called_with(1, 2, 3, a='fish', b='nothing')
320331

321332

333+
def test_assert_called_with_any(self):
334+
m = MagicMock()
335+
m(MagicMock())
336+
m.assert_called_with(mock.ANY)
337+
338+
322339
def test_assert_called_with_function_spec(self):
323340
def f(a, b, c, d=None):
324341
pass

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ Core and Builtins
9494
Library
9595
-------
9696

97+
- Issue #25195: Fix a regression in mock.MagicMock. _Call is a subclass of
98+
tuple (changeset 3603bae63c13 only works for classes) so we need to
99+
implement __ne__ ourselves. Patch by Andrew Plummer.
100+
97101
- Issue #26644: Raise ValueError rather than SystemError when a negative
98102
length is passed to SSLSocket.recv() or read().
99103

0 commit comments

Comments
 (0)