Description
Hi,
basilisp test
always emits a PytestAssertRewriteWarning
when run in a Basilisp project:
PytestAssertRewriteWarning: Module already imported so cannot be rewritten: basilisp
To reproduce:
- Create a new project with
Poetry
and addbasilisp
andpytest
:
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
...
- 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:
-
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 callingpytest.main()
. -
Unload the basilisp module before calling pytest:
Usingdel 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. -
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