From d53730ba8c3bd7af4b310553a4d9c5cc70996dca Mon Sep 17 00:00:00 2001 From: pctablet505 Date: Wed, 15 Jul 2026 16:05:47 +0000 Subject: [PATCH 1/3] Avoid PytestAssertRewriteWarning in tests using runpytest 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 #1227 FreeBSD fix remains effective. Fixes #1334 --- tests/markers/test_class_scope.py | 2 +- tests/markers/test_function_scope.py | 4 +++- tests/markers/test_module_scope.py | 2 +- tests/markers/test_session_scope.py | 2 +- tests/test_event_loop_fixture.py | 4 ---- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/markers/test_class_scope.py b/tests/markers/test_class_scope.py index 28f7187c..4ee6951f 100644 --- a/tests/markers/test_class_scope.py +++ b/tests/markers/test_class_scope.py @@ -274,5 +274,5 @@ class TestClass: async def test_anything(self): pass """)) - result = pytester.runpytest("--asyncio-mode=strict") + result = pytester.runpytest("--asyncio-mode=strict", "--assert=plain") result.assert_outcomes(warnings=0, passed=1) diff --git a/tests/markers/test_function_scope.py b/tests/markers/test_function_scope.py index 180d5c71..1e797895 100644 --- a/tests/markers/test_function_scope.py +++ b/tests/markers/test_function_scope.py @@ -72,7 +72,9 @@ def test_warns_when_scope_argument_is_present(pytester: Pytester): async def test_warns(): ... """)) - result = pytester.runpytest("--asyncio-mode=strict", "-W", "default") + result = pytester.runpytest( + "--asyncio-mode=strict", "-W", "default", "--assert=plain" + ) result.assert_outcomes(passed=1, warnings=1) result.stdout.fnmatch_lines("*DeprecationWarning*") diff --git a/tests/markers/test_module_scope.py b/tests/markers/test_module_scope.py index a2662812..c1e6ff99 100644 --- a/tests/markers/test_module_scope.py +++ b/tests/markers/test_module_scope.py @@ -281,5 +281,5 @@ def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_be async def test_anything(): pass """)) - result = pytester.runpytest("--asyncio-mode=strict") + result = pytester.runpytest("--asyncio-mode=strict", "--assert=plain") result.assert_outcomes(warnings=0, passed=1) diff --git a/tests/markers/test_session_scope.py b/tests/markers/test_session_scope.py index 24566c5e..6cd85a55 100644 --- a/tests/markers/test_session_scope.py +++ b/tests/markers/test_session_scope.py @@ -392,5 +392,5 @@ def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_be async def test_anything(): pass """)) - result = pytester.runpytest("--asyncio-mode=strict") + result = pytester.runpytest("--asyncio-mode=strict", "--assert=plain") result.assert_outcomes(warnings=0, passed=1) diff --git a/tests/test_event_loop_fixture.py b/tests/test_event_loop_fixture.py index 8902511b..08355f04 100644 --- a/tests/test_event_loop_fixture.py +++ b/tests/test_event_loop_fixture.py @@ -13,8 +13,6 @@ def test_event_loop_fixture_handles_unclosed_async_gen( import asyncio import pytest - pytest_plugins = 'pytest_asyncio' - @pytest.mark.asyncio async def test_something(): async def generator_fn(): @@ -67,8 +65,6 @@ def test_event_loop_fixture_asyncgen_error( import asyncio import pytest - pytest_plugins = 'pytest_asyncio' - @pytest.mark.asyncio async def test_something(): # mock shutdown_asyncgen failure From cacf3617199196c9f4c38c972712086108c7fa0c Mon Sep 17 00:00:00 2001 From: pctablet505 Date: Thu, 16 Jul 2026 11:24:34 +0530 Subject: [PATCH 2/3] docs: add changelog fragment for #1334 --- changelog.d/1334.downstream.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/1334.downstream.rst diff --git a/changelog.d/1334.downstream.rst b/changelog.d/1334.downstream.rst new file mode 100644 index 00000000..ba2e2505 --- /dev/null +++ b/changelog.d/1334.downstream.rst @@ -0,0 +1 @@ +Avoided a ``PytestAssertRewriteWarning`` in the test suite when pytest-asyncio is already imported, such as when tests are run against a system-installed copy on Guix or FreeBSD. From c4a6ed4595f6e707d2bbc88965b337c33017f155 Mon Sep 17 00:00:00 2001 From: pctablet505 Date: Mon, 27 Jul 2026 16:54:11 +0530 Subject: [PATCH 3/3] Use --assert=plain in the remaining two affected tests 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. --- changelog.d/1334.downstream.rst | 2 +- tests/test_event_loop_fixture.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/changelog.d/1334.downstream.rst b/changelog.d/1334.downstream.rst index ba2e2505..653c9adc 100644 --- a/changelog.d/1334.downstream.rst +++ b/changelog.d/1334.downstream.rst @@ -1 +1 @@ -Avoided a ``PytestAssertRewriteWarning`` in the test suite when pytest-asyncio is already imported, such as when tests are run against a system-installed copy on Guix or FreeBSD. +Avoided a ``PytestAssertRewriteWarning`` in the test suite when it is run from an unpacked sdist. The sdist ships ``pytest_asyncio.egg-info/SOURCES.txt``, which lists ``tests/__init__.py``, so pytest marks the ``tests`` package for assertion rewriting even though it has already been imported by the outer session. This affects downstream distributors such as Guix. diff --git a/tests/test_event_loop_fixture.py b/tests/test_event_loop_fixture.py index 08355f04..e27d27b5 100644 --- a/tests/test_event_loop_fixture.py +++ b/tests/test_event_loop_fixture.py @@ -22,7 +22,9 @@ async def generator_fn(): gen = generator_fn() await gen.__anext__() """)) - result = pytester.runpytest("--asyncio-mode=strict", "-W", "default") + result = pytester.runpytest( + "--asyncio-mode=strict", "-W", "default", "--assert=plain" + ) result.assert_outcomes(passed=1, warnings=0) @@ -73,5 +75,7 @@ async def fail(): raise RuntimeError("mock error cleaning up...") loop.shutdown_asyncgens = fail """)) - result = pytester.runpytest("--asyncio-mode=strict", "-W", "default") + result = pytester.runpytest( + "--asyncio-mode=strict", "-W", "default", "--assert=plain" + ) result.assert_outcomes(passed=1, warnings=1)