Skip to content

basilisp test always emits a PytestAssertRewriteWarning message #1252

Open
@ikappaki

Description

@ikappaki

Hi,

basilisp test always emits a PytestAssertRewriteWarning when run in a Basilisp project:

PytestAssertRewriteWarning: Module already imported so cannot be rewritten: basilisp

To reproduce:

  1. Create a new project with Poetry and add basilisp and pytest:
PS > poetry new basproj
Created package basproj in basproj
PS > cd basproj
PS basproj> poetry add basilisp pytest
Creating virtualenv basproj in basproj\.venv
Using version ^0.3.8 for basilisp
Using version ^8.3.5 for pytest
...
  1. run basilisp test, the above warning is emitted:
PS basproj> poetry run basilisp test
================================================= test session starts 
platform win32 -- Python 3.11.4, pytest-8.3.5, pluggy-1.5.0
rootdir: basproj
configfile: pyproject.toml
plugins: basilisp-0.3.8
collected 0 items

================================================== warnings summary 
.venv\Lib\site-packages\_pytest\config\__init__.py:1277
  basproj\.venv\Lib\site-packages\_pytest\config\__init__.py:1277: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: basilisp
    self._mark_plugins_for_rewrite(hook)

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

The warning occurs because basilisp declares a pytest plugin in its pyproject.toml:

[tool.poetry.plugins.pytest11]
basilisp_test_runner = "basilisp.contrib.pytest.testrunner"

which results in the following entry point in the installed distribution:

# .venv/Lib/site-packages/basilisp-0.3.8.dist-info/entry_points.txt
[console_scripts]
basilisp=basilisp.cli:invoke_cli
basilisp-run=basilisp.cli:run_script

[pytest11]
basilisp_test_runner=basilisp.contrib.pytest.testrunner

As per assertion rewriting doc, any module registered as a [pytest11] plugin is subject to assertion rewriting. However, in cli.py, basilisp is imported before pytest.main() is called, so pytest can't reimport the module for the purpose of rewriting its assertions, hence the warning.

Options I can think of:

  1. Silence the warning:
    Since assertion rewriting is likely unnecessary for the Basilisp test runner plugin, this warning can probably be safely ignore. We can explicitly suppress this warning before calling pytest.main().

  2. Unload the basilisp module before calling pytest:
    Using del sys.modules["basilisp"] would technically allow pytest to reimport the module and rewrite it, but this is risky and could lead to inconsistencies, especially if anything from basilisp has already been used.

  3. Extract the test runner plugin into a separate package:
    This avoids the warning entirely but comes at the cost of creating and maintaining a new separate basilisp-testrunner package.

I'm leaning toward option 1, and plan to raise a PR for it. If you have a better idea or prefer a different solution, please feel free to suggest.

Thanks

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