]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Remove AnyCompare and use call objects everywhere.
authorXtreak <tir.karthi@gmail.com>
Mon, 19 Aug 2019 09:41:01 +0000 (15:11 +0530)
committerElizabeth Uselton <elizabeth.uselton@rover.com>
Thu, 12 Sep 2019 05:46:47 +0000 (22:46 -0700)
Lib/unittest/mock.py
Lib/unittest/test/testmock/testasync.py

index f9789e5e85a97c80cd11230f1d90ef648ff4b8da..49d9fc7584180c27e2c80b2a7dd03ed4fd5b627b 100644 (file)
@@ -864,7 +864,7 @@ class NonCallableMock(Base):
         def _error_message():
             msg = self._format_mock_failure_message(args, kwargs)
             return msg
-        expected = self._call_matcher(_Call((args, kwargs)))
+        expected = self._call_matcher(_Call((args, kwargs), two=True))
         actual = self._call_matcher(self.call_args)
         if actual != expected:
             cause = expected if isinstance(expected, Exception) else None
@@ -927,9 +927,9 @@ class NonCallableMock(Base):
         `assert_called_with` and `assert_called_once_with` that only pass if
         the call is the most recent one."""
         expected = self._call_matcher(_Call((args, kwargs), two=True))
-        cause = expected if isinstance(expected, Exception) else None
         actual = [self._call_matcher(c) for c in self.call_args_list]
-        if cause or expected not in _AnyComparer(actual):
+        if expected not in actual:
+            cause = expected if isinstance(expected, Exception) else None
             expected_string = self._format_mock_call_signature(args, kwargs)
             raise AssertionError(
                 '%s call not found' % expected_string
@@ -982,23 +982,6 @@ class NonCallableMock(Base):
         return f"\n{prefix}: {safe_repr(self.mock_calls)}."
 
 
-class _AnyComparer(list):
-    """A list which checks if it contains a call which may have an
-    argument of ANY, flipping the components of item and self from
-    their traditional locations so that ANY is guaranteed to be on
-    the left."""
-    def __contains__(self, item):
-        for _call in self:
-            if len(item) != len(_call):
-                continue
-            if all([
-                expected == actual
-                for expected, actual in zip(item, _call)
-            ]):
-                return True
-        return False
-
-
 def _try_iter(obj):
     if obj is None:
         return obj
@@ -2172,7 +2155,7 @@ class AsyncMockMixin(Base):
         """
         expected = self._call_matcher(_Call((args, kwargs), two=True))
         actual = [self._call_matcher(c) for c in self.await_args_list]
-        if expected not in _AnyComparer(actual):
+        if expected not in actual:
             cause = expected if isinstance(expected, Exception) else None
             expected_string = self._format_mock_call_signature(args, kwargs)
             raise AssertionError(
index ec6809cbd9490d6133e5b2083819ef307e19c736..67de767849460ed0cd7c3fe5e583b59580e3e0ad 100644 (file)
@@ -184,6 +184,10 @@ class AsyncAutospecTest(unittest.TestCase):
         spec.assert_awaited_with(1, 2, c=3)
         spec.assert_awaited()
 
+        with self.assertRaises(AssertionError):
+            spec.assert_any_await(e=1)
+
+
     def test_patch_with_autospec(self):
 
         async def test_async():