@@ -162,40 +162,24 @@ def pytest_fixture_setup(fixturedef: pytest.FixtureDef[Any], request: pytest.Fix
162162 result = yield
163163
164164 # Only do the magic if in the area of our interest & only for fixtures making the event loops.
165- # TODO: rewrite to simpler `if` & match-case when Python 3.10 is dropped (≈October 2026).
166165 should_patch = _should_patch (fixturedef , request )
167- is_loop = isinstance (result , asyncio .BaseEventLoop )
168- is_runner = False if sys .version_info < (3 , 11 ) else isinstance (result , asyncio .Runner )
169- is_bp_runner = False if sys .version_info < (3 , 11 ) else isinstance (result , asyncio .Runner )
170-
171- # We avoid extra dependencies, but pytest-asyncio>=1.1.0 uses it, and we need to detect it.
172- # TODO: remove when Python 3.10 is dropped (≈October 2026).
173- try :
174- from backports .asyncio .runner import Runner as bp_Runner
175- except ImportError :
176- is_bp_runner = False
177- else :
178- is_bp_runner = isinstance (result , bp_Runner )
179166
180167 # Patch the event loop at creation — even if unused and not enabled. We cannot patch later
181168 # in the middle of the run: e.g. for a session-scoped loop used in a few tests out of many.
182169 # NB: For the lowest "function" scope, we still cannot decide which options to use, since
183170 # we do not know yet if it will be the running loop or not — so we cannot optimize here
184171 # in order to patch-and-configure only once; we must patch here & configure+activate later.
185- if should_patch and (is_loop or is_runner or is_bp_runner ):
186- if isinstance (result , asyncio .BaseEventLoop ):
187- patchers .patch_event_loop (result , _enabled = False )
188- elif sys .version_info >= (3 , 11 ) and isinstance (result , asyncio .Runner ):
189- # Available only in python>=3.11, but mandatory for python>=3.14.
190- # The runner of pytest-asyncio is already entered, which means the loop is created.
191- # Even if not created, we cannot postpone the loop creation, so we create it here.
192- loop = result .get_loop ()
193- if isinstance (loop , asyncio .BaseEventLoop ):
194- patchers .patch_event_loop (loop , _enabled = False )
195- elif is_bp_runner : # TODO: drop this branch with Python 3.10
196- loop = result .get_loop ()
197- if isinstance (loop , asyncio .BaseEventLoop ):
198- patchers .patch_event_loop (loop , _enabled = False )
172+ if should_patch :
173+ match result :
174+ case asyncio .BaseEventLoop ():
175+ patchers .patch_event_loop (result , _enabled = False )
176+ case asyncio .Runner ():
177+ # Available only in python>=3.11, but mandatory for python>=3.14.
178+ # The runner of pytest-asyncio is already entered, which means the loop is created.
179+ # Even if not created, we cannot postpone the loop creation, so we create it here.
180+ loop = result .get_loop ()
181+ if isinstance (loop , asyncio .BaseEventLoop ):
182+ patchers .patch_event_loop (loop , _enabled = False )
199183
200184 return result
201185
0 commit comments