python config_error ai_generated true

AssertionError: assert False + where False = <built-in method __eq__ of type object at 0x...>(1, 2) # note: assertion rewriting is disabled; enable with --assert=rewrite

ID: python/pytest-assertion-introspection-disabled

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-08-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
6.x active — — —
7.x active — — —
8.x active — — —

Root Cause

pytest's assertion rewriting is disabled, either because --assert=plain was passed, the test module was imported via importlib without pytest's meta path finder, or PYTEST_DISABLE_PLUGIN_AUTOLOAD interfered.

generic

中文

pytest 的断言重写被禁用,可能是因为传入了 --assert=plain、测试模块通过 importlib 导入而未经过 pytest 的 meta path finder,或 PYTEST_DISABLE_PLUGIN_AUTOLOAD 干扰。

Workarounds

  1. 95% success
    # pytest.ini
    [pytest]
    addopts = --assert=rewrite
    # verify with: pytest --version -v | grep assertion
  2. 85% success
    # Instead of: import tests.test_foo
    # Run: pytest tests/test_foo.py
    # and remove any `importlib.import_module('tests.test_foo')` from conftest.py

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Verbosity increases output but does not re-enable assertion rewriting; the useful diff remains missing.

  2. 65% fail

    Manual prints work but clutter tests and don't restore pytest's rich introspection for all assertions.