Skip to content

Commit 240d314

Browse files
nicoddemusRonnyPfannschmidt
authored andcommitted
copy test and changelog from #5607 by @Niccodemus
1 parent c98e7ae commit 240d314

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

changelog/5606.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed internal error when test functions were patched with objects that cannot be compared
2+
for truth values against others, like ``numpy`` arrays.

testing/python/integration.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,34 @@ def test_hello_mock(self, abspath):
178178
reprec = testdir.inline_run()
179179
reprec.assertoutcome(passed=2)
180180

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+
181209
def test_mock(self, testdir):
182210
pytest.importorskip("mock", "1.0.1")
183211
testdir.makepyfile(

0 commit comments

Comments
 (0)