Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PytestAssertRewriteWarning when including a plugin via 'pytest_plugins' and importing #11150

Closed
LewisGaul opened this issue Jun 30, 2023 · 4 comments

Comments

@LewisGaul
Copy link

Warning when running pytest:
PytestAssertRewriteWarning: Module already imported so cannot be rewritten: plugin

Steps to reproduce

  • Create a local plugin, e.g. empty plugin.py file.
  • Create a test that includes the plugin via pytest_plugins and also imports the plugin:
import plugin
pytest_plugins = ("plugin",)
  • Run the tests:
$pytest test.py
=============================================== test session starts ================================================
platform linux -- Python 3.8.10, pytest-7.4.0, pluggy-1.2.0
rootdir: /home/legaul/tmp/pytest-issue
collected 0 items

================================================= warnings summary =================================================
venv/lib/python3.8/site-packages/_pytest/config/__init__.py:755
  /home/legaul/tmp/pytest-issue/venv/lib/python3.8/site-packages/_pytest/config/__init__.py:755: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: plugin
    self.import_plugin(import_spec)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================ 1 warning in 0.01s ================================================

Extra information

Obviously this is a contrived minimal example; a more realistic scenario is:

  • conftest.py contains pytest_plugins = (...)
  • Some indirect import of the plugin, e.g. for the purpose of type annotations

This is not seen when loading the plugin using pytest -p plugin instead (since the import happens earlier?).

Perhaps related to #10230?

@The-Compiler
Copy link
Member

See the relevant docs:

Plugins imported by pytest_plugins will also automatically be marked for assertion rewriting (see pytest.register_assert_rewrite()). However for this to have any effect the module must not be imported already; if it was already imported at the time the pytest_plugins statement is processed, a warning will result and assertions inside the plugin will not be rewritten. To fix this you can either call pytest.register_assert_rewrite() yourself before the module is imported, or you can arrange the code to delay the importing until after the plugin is registered.

@The-Compiler The-Compiler closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2023
@DeflateAwning
Copy link

This is not working with pytest_jsonreport:

import pytest
pytest.register_assert_rewrite('pytest_jsonreport', 'JSONReport', 'json_report_plugin') # Source: https://github.com/pytest-dev/pytest/issues/11150; call before importing pytest_jsonreport
from pytest_jsonreport.plugin import JSONReport
pytest.register_assert_rewrite('pytest_jsonreport', 'JSONReport', 'json_report_plugin') # Source: https://github.com/pytest-dev/pytest/issues/11150; call before importing pytest_jsonreport

Any suggestions what I'm doing wrong? @The-Compiler

@The-Compiler
Copy link
Member

@DeflateAwning Sorry, this got lost in my inbox. You should probably call pytest.register_assert_rewrite('pytest_jsonreport.plugin') because that's what you import.

If it's not that, make sure it really didn't get imported before, e.g. via assert "pytest_jsonreport" not in sys.modules and assert "pytest_jsonreport.plugin" not in sys.modules.

@DeflateAwning
Copy link

Weird, still didn't work

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

No branches or pull requests

3 participants