Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PytestAssertRewriteWarning when installing a plugin in editable mode #10230

@GergelyKalmar

Description

@GergelyKalmar

Create an empty plugin (pytest-plugin) as follows:

pyproject.toml

[build-system]
requires = ['setuptools']
build-backend = 'setuptools.build_meta'

[project]
name = 'pytest-plugin'
version = '0.0.1'

[project.entry-points.pytest11]
plugin = 'pytest_plugin.main'

requirements.txt

pip==22.2.2
pytest==7.1.2

pytest_plugin/main.py

"""
An empty plugin.
"""

Install the plugin in editable mode:

> pip install -e .

Run pytest:

> pytest
=== test session starts ===
platform linux -- Python 3.8.10, pytest-7.1.2, pluggy-1.0.0
rootdir: .../pytest-plugin
plugins: plugin-0.0.1
collected 0 items

=== warnings summary ===
.../pytest-plugin/lib/python3.8/site-packages/_pytest/config/__init__.py:1129
  .../pytest-plugin/lib/python3.8/site-packages/_pytest/config/__init__.py:1129: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: __editable___pytest_plugin_0_0_1_finder
    self._mark_plugins_for_rewrite(hook)

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

The issue seems to be that pytest tries re-writing the module under site-packages/__editable___pytest_plugin_0_0_1_finder.py which I think is created by pip. Note that a new mechanism for pyproject.toml-based editable builds was introduced in https://pip.pypa.io/en/stable/news/#v21-3, which I believe doesn't use the egg-based editable mechanism anymore.

It might be that it would be enough to update https://github.com/pytest-dev/pytest/blob/main/src/_pytest/config/__init__.py#L799 to exclude the editable installation finder modules (similar to how setup.py files are excluded).

> pip list
Package       Version Editable project location
------------- ------- --------------------------------------------
attrs         22.1.0
iniconfig     1.1.1
packaging     21.3
pip           22.2.2
pkg_resources 0.0.0
pluggy        1.0.0
py            1.11.0
pyparsing     3.0.9
pytest        7.1.2
pytest-plugin 0.0.1   .../pytest-plugin
setuptools    44.0.0
tomli         2.0.1

> uname -a
Linux flux 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions