Avoid PytestAssertRewriteWarning in tests using runpytest - #1508
Avoid PytestAssertRewriteWarning in tests using runpytest#1508pctablet505 wants to merge 3 commits into
Conversation
Adds --assert=plain to inner runpytest calls for tests that check warning counts, and removes redundant pytest_plugins declarations in generated test code. Keeps in-process execution so the pytest-dev#1227 FreeBSD fix remains effective. Fixes pytest-dev#1334
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1508 +/- ##
=======================================
Coverage 94.50% 94.50%
=======================================
Files 2 2
Lines 510 510
Branches 62 62
=======================================
Hits 482 482
Misses 22 22
Partials 6 6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
seifertm
left a comment
There was a problem hiding this comment.
Thanks for the initiative.
The code changes do make sense superficially, but I'm reluctant to integrate them when there's no proof the patch actually solves the problem. Personally, I also don't understand the nature of what causes the issue. In order to progress this, I think we need to find answers to one or more of the following questions:
What is the reason assertion rewriting or setting pytest_plugins causes problems in the first place?
Can you provide a reproducer that verifies this patch fixes the linked GUIX issue?
Can you provide a reproducer that verifies for FreeBSD that this patch does not regress #1227 ?
The two tests in tests/test_event_loop_fixture.py were still failing on an sdist-based install: dropping the redundant pytest_plugins line does not suppress the warning, because the warning comes from pytest's entry-point scan (Config._mark_plugins_for_rewrite), which flags the already-imported 'tests' package rather than 'pytest_asyncio'. Verified against a reproduction of the Guix build environment (sdist built and installed in-tree, suite run via the pytest console script): 2 failed before this commit, 298 passed after.
|
You were right to hold off — the patch as I submitted it only fixed 4 of the 6 tests. Pushed a follow-up. Evidence below. CauseThe give-away is in the Guix log: the module pytest names is So $ python -c "
from _pytest.config import _iter_rewritable_modules
lines = [l.strip() for l in open('pytest_asyncio.egg-info/SOURCES.txt') if l.strip()]
print('sdist egg-info SOURCES.txt ->', sorted(set(_iter_rewritable_modules(lines))))
import zipfile
zf = zipfile.ZipFile('dist/pytest_asyncio-1.3.0-py3-none-any.whl')
rec = [l.split(',')[0] for l in zf.read('pytest_asyncio-1.3.0.dist-info/RECORD').decode().splitlines() if l]
print('installed wheel RECORD ->', sorted(set(_iter_rewritable_modules(rec))))
"
sdist egg-info SOURCES.txt -> ['pytest_asyncio', 'tests']
installed wheel RECORD -> ['pytest_asyncio']Then Why CI never sees it: at config time the build dir isn't on Reproduceruv venv venv --python 3.11
uv pip install --python ./venv/bin/python "pytest==9.0.2" hypothesis setuptools \
setuptools_scm wheel "typing-extensions>=4.12" "backports-asyncio-runner>=1.1,<2"
curl -sLO https://files.pythonhosted.org/packages/source/p/pytest-asyncio/pytest_asyncio-1.3.0.tar.gz
tar xzf pytest_asyncio-1.3.0.tar.gz && cd pytest_asyncio-1.3.0
# build in-tree, like Guix's build phase — this re-reads the shipped SOURCES.txt
../venv/bin/python -c "from setuptools.build_meta import build_wheel; build_wheel('dist')"
uv pip install --python ../venv/bin/python --no-deps dist/*.whl
../venv/bin/pytest # -> 6 failed, 234 passed
../venv/bin/python -m pytest # -> 240 passed
Which half of the patch does the workFive variants, clean restore of the pristine sdist each time, full suite:
Worth noting this isn't a new idiom: On #1227That was a different failure — Checked empirically too, with 0.23.8 in a directory a subprocess finds on The subprocess reproduces #1227 on demand; Pushed
One thing for you to decide: the more principled fix is probably packaging-side — not shipping |
Fixes #1334
Pass
--assert=plainto the in-processrunpytestcalls for the six tests that emitPytestAssertRewriteWarning, keeping the #1227 in-process fix intact. Also drop the explicitpytest_pluginsassignment in the two event-loop fixture tests so they do not mark the already-loaded plugin for assertion rewriting.