Skip to content

Comments

swev-id: pytest-dev__pytest-5631 fix sentinel identity for WeirdEq (#10)#12

Open
casey-brooks wants to merge 1 commit intopytest-dev__pytest-5631from
fix/pytest-5631-sentinel-identity
Open

swev-id: pytest-dev__pytest-5631 fix sentinel identity for WeirdEq (#10)#12
casey-brooks wants to merge 1 commit intopytest-dev__pytest-5631from
fix/pytest-5631-sentinel-identity

Conversation

@casey-brooks
Copy link

Summary

  • fix ValueError when collecting tests that patch an array #10 by ensuring sentinel detection only uses identity checks, avoiding ambiguous equality on numpy-like sentinels
  • add a minimal regression test that patches os.path.abspath with a WeirdEq sentinel to reproduce the failure during collection

Reproduction snippet

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

Run with:

PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 .venv/bin/pytest --collect-only testing/test_patch_array_sentinel.py

Observed failure before the fix:

============================= test session starts ==============================
platform linux -- Python 3.8.16, pytest-5.0.2.dev58+gcb828ebe7, py-1.11.0, pluggy-0.13.1
rootdir: /workspace/pytest, inifile: tox.ini
collected 0 items / 1 errors

==================================== ERRORS ====================================
____________ ERROR collecting testing/test_patch_array_sentinel.py _____________
testing/test_patch_array_sentinel.py:7: in __bool__
    raise ValueError("ambiguous truth")
E   ValueError: ambiguous truth
=========================== short test summary info ============================
FAILED testing/test_patch_array_sentinel.py - ValueError: ambiguous truth
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.04 seconds ============================

Fix details

  • num_mock_patch_args now matches sentinel replacements using is comparisons against the sentinel singletons instead of equality
  • avoids executing user-defined __eq__ implementations when counting sentinel arguments, restoring safe collection

Testing

  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 .venv/bin/pytest --collect-only testing/test_patch_array_sentinel.py
  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 .venv/bin/pytest testing/test_patch_array_sentinel.py
  • flake8 src/_pytest/compat.py testing/test_patch_array_sentinel.py

@casey-brooks casey-brooks requested a review from a team December 25, 2025 20:13
@casey-brooks
Copy link
Author

Test & Lint Summary

  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 .venv/bin/pytest --collect-only testing/test_patch_array_sentinel.py (collected 1 test, 0 errors)
  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 .venv/bin/pytest testing/test_patch_array_sentinel.py (1 passed)
  • flake8 src/_pytest/compat.py testing/test_patch_array_sentinel.py (no issues)

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

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

Looks solid—thanks for the regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants