File tree Expand file tree Collapse file tree 3 files changed +42
-7
lines changed Expand file tree Collapse file tree 3 files changed +42
-7
lines changed Original file line number Diff line number Diff line change
1
+ Fixed internal error when test functions were patched with objects that cannot be compared
2
+ for truth values against others, like ``numpy `` arrays.
Original file line number Diff line number Diff line change @@ -64,13 +64,18 @@ def num_mock_patch_args(function):
64
64
patchings = getattr (function , "patchings" , None )
65
65
if not patchings :
66
66
return 0
67
- mock_modules = [sys .modules .get ("mock" ), sys .modules .get ("unittest.mock" )]
68
- if any (mock_modules ):
69
- sentinels = [m .DEFAULT for m in mock_modules if m is not None ]
70
- return len (
71
- [p for p in patchings if not p .attribute_name and p .new in sentinels ]
72
- )
73
- return len (patchings )
67
+
68
+ mock_sentinel = getattr (sys .modules .get ("mock" ), "DEFAULT" , object ())
69
+ ut_mock_sentinel = getattr (sys .modules .get ("unittest.mock" ), "DEFAULT" , object ())
70
+
71
+ return len (
72
+ [
73
+ p
74
+ for p in patchings
75
+ if not p .attribute_name
76
+ and (p .new is mock_sentinel or p .new is ut_mock_sentinel )
77
+ ]
78
+ )
74
79
75
80
76
81
def getfuncargnames (function , is_method = False , cls = None ):
Original file line number Diff line number Diff line change @@ -178,6 +178,34 @@ def test_hello_mock(self, abspath):
178
178
reprec = testdir .inline_run ()
179
179
reprec .assertoutcome (passed = 2 )
180
180
181
+ def test_mock_sentinel_check_against_numpy_like (self , testdir ):
182
+ """Ensure our function that detects mock arguments compares against sentinels using
183
+ identity to circumvent objects which can't be compared with equality against others
184
+ in a truth context, like with numpy arrays (#5606).
185
+ """
186
+ testdir .makepyfile (
187
+ dummy = """
188
+ class NumpyLike:
189
+ def __init__(self, value):
190
+ self.value = value
191
+ def __eq__(self, other):
192
+ raise ValueError("like numpy, cannot compare against others for truth")
193
+ FOO = NumpyLike(10)
194
+ """
195
+ )
196
+ testdir .makepyfile (
197
+ """
198
+ from unittest.mock import patch
199
+ import dummy
200
+ class Test(object):
201
+ @patch("dummy.FOO", new=dummy.NumpyLike(50))
202
+ def test_hello(self):
203
+ assert dummy.FOO.value == 50
204
+ """
205
+ )
206
+ reprec = testdir .inline_run ()
207
+ reprec .assertoutcome (passed = 1 )
208
+
181
209
def test_mock (self , testdir ):
182
210
pytest .importorskip ("mock" , "1.0.1" )
183
211
testdir .makepyfile (
You can’t perform that action at this time.
0 commit comments