diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index d238061b4ab..b3d2cbdb669 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -68,7 +68,12 @@ def num_mock_patch_args(function): if any(mock_modules): sentinels = [m.DEFAULT for m in mock_modules if m is not None] 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 any(p.new is s for s in sentinels) + ] ) return len(patchings) diff --git a/testing/test_patch_array_sentinel.py b/testing/test_patch_array_sentinel.py new file mode 100644 index 00000000000..0bf133d7296 --- /dev/null +++ b/testing/test_patch_array_sentinel.py @@ -0,0 +1,15 @@ +from unittest.mock import patch + + +class WeirdEq: + class Amb: + def __bool__(self): + raise ValueError("ambiguous truth") + + def __eq__(self, other): + return WeirdEq.Amb() + + +@patch("os.path.abspath", new=WeirdEq()) +def test_collect_ok(): + assert True