pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Assertion rewriting issue with editable-installed plugins

Open bluetech opened this issue 1 year ago • 0 comments

Problem

When I look into/debug external plugins, my workflow is:

  • Create a venv
  • Run pip install -e . (this is with a recent pip and setuptools backend, so is using "new style" PEP 660 editable installs. editable_mode=strict doesn't seem to make a difference)
  • Run pytest

While doing this for several plugins recently (e.g. pytest-asyncio), I've noticed pytest issues a warning:

../../../../home/ran/src/pytest/src/_pytest/config/__init__.py:759
  /home/ran/src/pytest/src/_pytest/config/__init__.py:759: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: pytest_asyncio
    self.import_plugin(import_spec)

Analysis

This is my understanding of why this happens:

  • pytest's assertion rewriting requires that a module be marked for rewrite before it is imported.
  • For external plugins this is done by the _mark_plugins_for_rewrite function.
  • This function iterates over all files of all installed distributions which have a pytest11 entry point, and marks their modules for rewrite if necessary.

This ends up now working with my workflow because the distribution.files doesn't contain any of the actual package python files. Relevant issues: https://github.com/python/cpython/issues/96144 https://github.com/pypa/packaging-problems/issues/620

Currently there is no solution from the packaging side. It might be possible to add a workaround on the pytest side, I'm not sure.

bluetech avatar Jan 06 '24 20:01 bluetech