pytest-dev / pytest-dev/pytest

Another way to disable rewrites?

Open
#5,473 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I am trying to add a pytest plugin to coverage.py. Because the plugin is in the coverage package, coverage/init.py will be imported when the plugin is imported. Because I'm using coverage to run pytest in the first place, coverage/__init__.py has already been imported. This leads to a warning:

PytestAssertRewriteWarning: Module already imported so cannot be rewritten: coverage

I can disable this warning by adding PYTEST_DONT_REWRITE to the coverage/__init__.py docstring, but then that word is visible in the docstring for my package. Ugly.

Much of pytest is controlled by specially named attributes in modules. It would be great if I could control rewriting by adding an attribute to my module instead of amending the docstring.

If needed, I can provide a reproducer that shows the warning.

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

Start with the reported coverage/init.py setup and the PytestAssertRewriteWarning, then create the reproducer mentioned in the issue. Review pytest's assertion-rewrite handling to determine how a module attribute could control rewriting. Done means the warning can be disabled through a module attribute without adding PYTEST_DONT_REWRITE to the package docstring, with coverage's behavior preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.