From 1a505aa7e90d6417c2697ee77c8bc8b74df5adab Mon Sep 17 00:00:00 2001 From: krishashah-03 Date: Mon, 16 Nov 2026 22:06:00 +0530 Subject: [PATCH 1/2] Allow event loop policies that reject set_event_loop (#1233) --- changelog.d/1233.fixed.rst | 1 + pytest_asyncio/plugin.py | 18 +++++++++++++++--- tests/test_set_event_loop.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 changelog.d/1233.fixed.rst diff --git a/changelog.d/1233.fixed.rst b/changelog.d/1233.fixed.rst new file mode 100644 index 00000000..992c0e26 --- /dev/null +++ b/changelog.d/1233.fixed.rst @@ -0,0 +1 @@ +Allow event loop policies that reject setting a loop on the current thread to run async tests. diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 32b1a834..df0264ba 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -1015,12 +1015,24 @@ def _scoped_runner( ) -> Iterator[Runner]: debug_mode = _get_asyncio_debug(request.config) with _restore_event_loop_policy(): + # Always hand the runner an explicit factory. Without one, the runner + # calls asyncio.set_event_loop() itself, which fails for event loop + # policies that manage the loop of the main thread on their own. + loop_factory = _asyncio_loop_factory or asyncio.new_event_loop runner = Runner( debug=debug_mode, - loop_factory=_asyncio_loop_factory, + loop_factory=loop_factory, ).__enter__() - if _asyncio_loop_factory is not None: + loop_is_set = False + try: _set_event_loop(runner.get_loop()) + loop_is_set = True + except RuntimeError: + # Some event loop policies manage their loop outside of + # asyncio.set_event_loop(). The runner can still execute the + # loop because it is available through asyncio.get_running_loop() + # while a coroutine is running. + pass try: yield runner except Exception as e: @@ -1038,7 +1050,7 @@ def _scoped_runner( RuntimeWarning, ) finally: - if _asyncio_loop_factory is not None: + if loop_is_set: _set_event_loop(None) return _scoped_runner diff --git a/tests/test_set_event_loop.py b/tests/test_set_event_loop.py index 2fdfc031..402d5b6b 100644 --- a/tests/test_set_event_loop.py +++ b/tests/test_set_event_loop.py @@ -6,6 +6,40 @@ from pytest import Pytester +def test_runner_works_with_policy_that_rejects_setting_loop(pytester: Pytester): + pytester.makeconftest(dedent("""\ + import asyncio + import warnings + + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + + class MainContextPolicy(asyncio.DefaultEventLoopPolicy): + def set_event_loop(self, loop): + if loop is not None: + raise RuntimeError("Thread already has a main context") + super().set_event_loop(loop) + + asyncio.set_event_loop_policy(MainContextPolicy()) + """)) + pytester.makepyfile(dedent("""\ + import asyncio + import pytest + + @pytest.mark.asyncio + async def test_first(): + assert asyncio.get_running_loop().is_running() + + @pytest.mark.asyncio + async def test_second(): + assert asyncio.get_running_loop().is_running() + """)) + + result = pytester.runpytest_subprocess() + + result.assert_outcomes(passed=2) + + @pytest.mark.parametrize( "test_loop_scope", ("function", "module", "package", "session"), From 707fbf92f9cef73969cd146133ebd6b66dce5164 Mon Sep 17 00:00:00 2001 From: krishashah-03 Date: Mon, 16 Nov 2026 22:16:00 +0530 Subject: [PATCH 2/2] Skip policy test on Python 3.16, where policies were removed --- tests/test_set_event_loop.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_set_event_loop.py b/tests/test_set_event_loop.py index 402d5b6b..8dd4168a 100644 --- a/tests/test_set_event_loop.py +++ b/tests/test_set_event_loop.py @@ -1,11 +1,16 @@ from __future__ import annotations +import sys from textwrap import dedent import pytest from pytest import Pytester +@pytest.mark.skipif( + sys.version_info >= (3, 16), + reason="The asyncio event loop policy system was removed in Python 3.16", +) def test_runner_works_with_policy_that_rejects_setting_loop(pytester: Pytester): pytester.makeconftest(dedent("""\ import asyncio