Skip to content

Commit 24973c0

Browse files
tirkarthiElizabethU
authored andcommitted
Remove AnyCompare and use call objects everywhere.
1 parent 38650c9 commit 24973c0

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
@@ -864,7 +864,7 @@ def assert_called_with(self, /, *args, **kwargs):
864864
def _error_message():
865865
msg = self._format_mock_failure_message(args, kwargs)
866866
return msg
867-
expected = self._call_matcher(_Call((args, kwargs)))
867+
expected = self._call_matcher(_Call((args, kwargs), two=True))
868868
actual = self._call_matcher(self.call_args)
869869
if actual != expected:
870870
cause = expected if isinstance(expected, Exception) else None
@@ -927,9 +927,9 @@ def assert_any_call(self, /, *args, **kwargs):
927927
`assert_called_with` and `assert_called_once_with` that only pass if
928928
the call is the most recent one."""
929929
expected = self._call_matcher(_Call((args, kwargs), two=True))
930-
cause = expected if isinstance(expected, Exception) else None
931930
actual = [self._call_matcher(c) for c in self.call_args_list]
932-
if cause or expected not in _AnyComparer(actual):
931+
if expected not in actual:
932+
cause = expected if isinstance(expected, Exception) else None
933933
expected_string = self._format_mock_call_signature(args, kwargs)
934934
raise AssertionError(
935935
'%s call not found' % expected_string
@@ -982,23 +982,6 @@ def _calls_repr(self, prefix="Calls"):
982982
return f"\n{prefix}: {safe_repr(self.mock_calls)}."
983983

984984

985-
class _AnyComparer(list):
986-
"""A list which checks if it contains a call which may have an
987-
argument of ANY, flipping the components of item and self from
988-
their traditional locations so that ANY is guaranteed to be on
989-
the left."""
990-
def __contains__(self, item):
991-
for _call in self:
992-
if len(item) != len(_call):
993-
continue
994-
if all([
995-
expected == actual
996-
for expected, actual in zip(item, _call)
997-
]):
998-
return True
999-
return False
1000-
1001-
1002985
def _try_iter(obj):
1003986
if obj is None:
1004987
return obj
@@ -2172,7 +2155,7 @@ def assert_any_await(self, /, *args, **kwargs):
21722155
"""
21732156
expected = self._call_matcher(_Call((args, kwargs), two=True))
21742157
actual = [self._call_matcher(c) for c in self.await_args_list]
2175-
if expected not in _AnyComparer(actual):
2158+
if expected not in actual:
21762159
cause = expected if isinstance(expected, Exception) else None
21772160
expected_string = self._format_mock_call_signature(args, kwargs)
21782161
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)