@@ -864,9 +864,9 @@ def assert_called_with(self, /, *args, **kwargs):
864
864
def _error_message ():
865
865
msg = self ._format_mock_failure_message (args , kwargs )
866
866
return msg
867
- expected = self ._call_matcher (( args , kwargs ))
867
+ expected = self ._call_matcher (_Call (( args , kwargs ) ))
868
868
actual = self ._call_matcher (self .call_args )
869
- if expected != actual :
869
+ if actual != expected :
870
870
cause = expected if isinstance (expected , Exception ) else None
871
871
raise AssertionError (_error_message ()) from cause
872
872
@@ -926,10 +926,10 @@ def assert_any_call(self, /, *args, **kwargs):
926
926
The assert passes if the mock has *ever* been called, unlike
927
927
`assert_called_with` and `assert_called_once_with` that only pass if
928
928
the call is the most recent one."""
929
- expected = self ._call_matcher ((args , kwargs ))
929
+ expected = self ._call_matcher (_Call ((args , kwargs ), two = True ))
930
+ cause = expected if isinstance (expected , Exception ) else None
930
931
actual = [self ._call_matcher (c ) for c in self .call_args_list ]
931
- if expected not in actual :
932
- cause = expected if isinstance (expected , Exception ) else None
932
+ if cause or expected not in _AnyComparer (actual ):
933
933
expected_string = self ._format_mock_call_signature (args , kwargs )
934
934
raise AssertionError (
935
935
'%s call not found' % expected_string
@@ -982,6 +982,22 @@ def _calls_repr(self, prefix="Calls"):
982
982
return f"\n { prefix } : { safe_repr (self .mock_calls )} ."
983
983
984
984
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
+
985
1001
986
1002
def _try_iter (obj ):
987
1003
if obj is None :
@@ -2133,9 +2149,9 @@ def _error_message():
2133
2149
msg = self ._format_mock_failure_message (args , kwargs , action = 'await' )
2134
2150
return msg
2135
2151
2136
- expected = self ._call_matcher (( args , kwargs ))
2152
+ expected = self ._call_matcher (_Call (( args , kwargs ), two = True ))
2137
2153
actual = self ._call_matcher (self .await_args )
2138
- if expected != actual :
2154
+ if actual != expected :
2139
2155
cause = expected if isinstance (expected , Exception ) else None
2140
2156
raise AssertionError (_error_message ()) from cause
2141
2157
@@ -2154,9 +2170,9 @@ def assert_any_await(self, /, *args, **kwargs):
2154
2170
"""
2155
2171
Assert the mock has ever been awaited with the specified arguments.
2156
2172
"""
2157
- expected = self ._call_matcher (( args , kwargs ))
2173
+ expected = self ._call_matcher (_Call (( args , kwargs ), two = True ))
2158
2174
actual = [self ._call_matcher (c ) for c in self .await_args_list ]
2159
- if expected not in actual :
2175
+ if expected not in _AnyComparer ( actual ) :
2160
2176
cause = expected if isinstance (expected , Exception ) else None
2161
2177
expected_string = self ._format_mock_call_signature (args , kwargs )
2162
2178
raise AssertionError (
0 commit comments