pytest-dev / pytest-dev/pytest

Assertion rewriting issue with editable-installed plugins

Open
#11,783 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: rewrite type: bug
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the warning with a recent pip/setuptools PEP 660 editable install, using an external plugin such as pytest-asyncio. Start at _mark_plugins_for_rewrite in src/_pytest/config/init.py and inspect how pytest uses distribution.files for pytest11 entry points; done means establishing a pytest-side path that avoids the warning when package files are absent.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.