Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix num_mock_patch_args to use identity comparison #5607

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/5606.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed internal error when test functions were patched with objects that cannot be compared
for truth values against others, like ``numpy`` arrays.
11 changes: 10 additions & 1 deletion src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,17 @@ def num_mock_patch_args(function):
mock_modules = [sys.modules.get("mock"), sys.modules.get("unittest.mock")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its a 2 element list, use a variable, and use 2 is checks at the end - its ridiculus to introduce that kind of helper function programming here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"ridiculous"

if any(mock_modules):
sentinels = [m.DEFAULT for m in mock_modules if m is not None]

def check_is_sentinel(o):
"""Check if the given object is a mock sentinel by identity to avoid possible
problematic comparisons (#5606)"""
for sentinel in sentinels:
if o is sentinel:
return True
return False

return len(
[p for p in patchings if not p.attribute_name and p.new in sentinels]
[p for p in patchings if not p.attribute_name and check_is_sentinel(p.new)]
)
return len(patchings)

Expand Down
31 changes: 31 additions & 0 deletions testing/python/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,37 @@ def test_hello_mock(self, abspath):
reprec = testdir.inline_run()
reprec.assertoutcome(passed=2)

def test_mock_sentinel_check_against_numpy_like(self, testdir):
"""Ensure our function that detects mock arguments compares against sentinels using
identity to circumvent objects which can't be compared with equality against others
in a truth context, like with numpy arrays (#5606).
"""
testdir.makepyfile(
dummy="""
class NumpyLike:
def __init__(self, value):
self.value = value

def __eq__(self, other):
raise ValueError("like numpy, cannot compare against others for truth")

FOO = NumpyLike(10)
"""
)
testdir.syspathinsert()
testdir.makepyfile(
"""
from unittest.mock import patch
import dummy
class Test(object):
@patch("dummy.FOO", new=dummy.NumpyLike(50))
def test_hello(self):
assert dummy.FOO.value == 50
"""
)
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)

def test_mock(self, testdir):
pytest.importorskip("mock", "1.0.1")
testdir.makepyfile(
Expand Down