@@ -171,7 +171,18 @@ def pytest_fixture_setup(fixturedef: pytest.FixtureDef[Any], request: pytest.Fix
171171 should_patch = _should_patch (fixturedef , request )
172172 is_loop = isinstance (result , asyncio .BaseEventLoop )
173173 is_runner = False if sys .version_info < (3 , 11 ) else isinstance (result , asyncio .Runner )
174- if should_patch and (is_loop or is_runner ):
174+ is_bp_runner = False if sys .version_info < (3 , 11 ) else isinstance (result , asyncio .Runner )
175+
176+ # We avoid extra dependencies, but pytest-asyncio>=1.1.0 uses it, and we need to detect it.
177+ # TODO: remove when Python 3.10 is dropped (≈October 2026).
178+ try :
179+ from backports .asyncio .runner import Runner as bp_Runner
180+ except ImportError :
181+ is_bp_runner = False
182+ else :
183+ is_bp_runner = isinstance (result , bp_Runner )
184+
185+ if should_patch and (is_loop or is_runner or is_bp_runner ):
175186
176187 # Populate the helper mapper of names-to-scopes, as used in the test hook below.
177188 if EVENT_LOOP_SCOPES not in request .session .stash :
@@ -193,6 +204,10 @@ def pytest_fixture_setup(fixturedef: pytest.FixtureDef[Any], request: pytest.Fix
193204 loop = result .get_loop ()
194205 if isinstance (loop , asyncio .BaseEventLoop ):
195206 patchers .patch_event_loop (loop , _enabled = False )
207+ elif is_bp_runner : # TODO: drop this branch with Python 3.10
208+ loop = result .get_loop ()
209+ if isinstance (loop , asyncio .BaseEventLoop ):
210+ patchers .patch_event_loop (loop , _enabled = False )
196211
197212 return result
198213
0 commit comments