Skip to content

Commit bc09989

Browse files
committed
Remove AnyCompare and use call objects everywhere.
1 parent 7a89117 commit bc09989

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

Lib/unittest/mock.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ def assert_called_with(self, /, *args, **kwargs):
869869
def _error_message():
870870
msg = self._format_mock_failure_message(args, kwargs)
871871
return msg
872-
expected = self._call_matcher(_Call((args, kwargs)))
872+
expected = self._call_matcher(_Call((args, kwargs), two=True))
873873
actual = self._call_matcher(self.call_args)
874874
if actual != expected:
875875
cause = expected if isinstance(expected, Exception) else None
@@ -932,9 +932,9 @@ def assert_any_call(self, /, *args, **kwargs):
932932
`assert_called_with` and `assert_called_once_with` that only pass if
933933
the call is the most recent one."""
934934
expected = self._call_matcher(_Call((args, kwargs), two=True))
935-
cause = expected if isinstance(expected, Exception) else None
936935
actual = [self._call_matcher(c) for c in self.call_args_list]
937-
if cause or expected not in _AnyComparer(actual):
936+
if expected not in actual:
937+
cause = expected if isinstance(expected, Exception) else None
938938
expected_string = self._format_mock_call_signature(args, kwargs)
939939
raise AssertionError(
940940
'%s call not found' % expected_string
@@ -987,23 +987,6 @@ def _calls_repr(self, prefix="Calls"):
987987
return f"\n{prefix}: {safe_repr(self.mock_calls)}."
988988

989989

990-
class _AnyComparer(list):
991-
"""A list which checks if it contains a call which may have an
992-
argument of ANY, flipping the components of item and self from
993-
their traditional locations so that ANY is guaranteed to be on
994-
the left."""
995-
def __contains__(self, item):
996-
for _call in self:
997-
if len(item) != len(_call):
998-
continue
999-
if all([
1000-
expected == actual
1001-
for expected, actual in zip(item, _call)
1002-
]):
1003-
return True
1004-
return False
1005-
1006-
1007990
def _try_iter(obj):
1008991
if obj is None:
1009992
return obj
@@ -2177,7 +2160,7 @@ def assert_any_await(self, /, *args, **kwargs):
21772160
"""
21782161
expected = self._call_matcher(_Call((args, kwargs), two=True))
21792162
actual = [self._call_matcher(c) for c in self.await_args_list]
2180-
if expected not in _AnyComparer(actual):
2163+
if expected not in actual:
21812164
cause = expected if isinstance(expected, Exception) else None
21822165
expected_string = self._format_mock_call_signature(args, kwargs)
21832166
raise AssertionError(

Lib/unittest/test/testmock/testasync.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ async def main():
184184
spec.assert_awaited_with(1, 2, c=3)
185185
spec.assert_awaited()
186186

187+
with self.assertRaises(AssertionError):
188+
spec.assert_any_await(e=1)
189+
190+
187191
def test_patch_with_autospec(self):
188192

189193
async def test_async():

0 commit comments

Comments
 (0)