Skip to content

Commit d36d28f

Browse files
committed
Deprecate hook
1 parent efd7a31 commit d36d28f

File tree

10 files changed

+79
-11
lines changed

10 files changed

+79
-11
lines changed

changelog.d/1419.deprecated.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Overriding the *event_loop_policy* fixture is deprecated. Use the ``pytest_asyncio_loop_factories`` hook instead.

docs/how-to-guides/multiple_loops.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
How to test with different event loops
33
======================================
44

5+
.. warning::
6+
7+
Overriding the *event_loop_policy* fixture is deprecated and will be removed in a future version of pytest-asyncio. Use the ``pytest_asyncio_loop_factories`` hook instead. See :doc:`custom_loop_factory` for details.
8+
59
Parametrizing the *event_loop_policy* fixture parametrizes all async tests. The following example causes all async tests to run multiple times, once for each event loop in the fixture parameters:
610

711
.. include:: multiple_loops_example.py

docs/how-to-guides/uvloop.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Using the event_loop_policy fixture
2424

2525
.. note::
2626

27-
``asyncio.AbstractEventLoopPolicy`` is deprecated as of Python 3.14 (removal planned for 3.16), and ``uvloop.EventLoopPolicy`` will be removed alongside it. Prefer the hook approach above.
27+
``asyncio.AbstractEventLoopPolicy`` is deprecated as of Python 3.14 (removal planned for 3.16), and ``uvloop.EventLoopPolicy`` will be removed alongside it. Overriding the *event_loop_policy* fixture is also deprecated in pytest-asyncio. Prefer the hook approach above.
2828

2929
For older versions of Python and uvloop, you can override the *event_loop_policy* fixture in your *conftest.py:*
3030

docs/reference/fixtures/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Fixtures
44

55
event_loop_policy
66
=================
7+
8+
.. warning::
9+
10+
Overriding the *event_loop_policy* fixture is deprecated and will be removed in a future version of pytest-asyncio. Use the ``pytest_asyncio_loop_factories`` hook instead. See :doc:`../hooks` for details.
11+
712
Returns the event loop policy used to create asyncio event loops.
813
The default return value is *asyncio.get_event_loop_policy().*
914

pytest_asyncio/plugin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,13 @@ def inner(*args, **kwargs):
919919

920920
@pytest.hookimpl(wrapper=True)
921921
def pytest_fixture_setup(fixturedef: FixtureDef, request) -> object | None:
922+
if (
923+
fixturedef.argname == "event_loop_policy"
924+
and fixturedef.func.__module__ != __name__
925+
):
926+
warnings.warn(
927+
PytestDeprecationWarning(_EVENT_LOOP_POLICY_FIXTURE_DEPRECATION_WARNING),
928+
)
922929
asyncio_mode = _get_asyncio_mode(request.config)
923930
if not _is_asyncio_fixture_function(fixturedef.func):
924931
if asyncio_mode == Mode.STRICT:
@@ -962,6 +969,12 @@ def pytest_fixture_setup(fixturedef: FixtureDef, request) -> object | None:
962969
mark.asyncio 'loop_factories' must be a non-empty sequence of strings.
963970
"""
964971

972+
_EVENT_LOOP_POLICY_FIXTURE_DEPRECATION_WARNING = """\
973+
Overriding the "event_loop_policy" fixture is deprecated \
974+
and will be removed in a future version of pytest-asyncio. \
975+
Use the "pytest_asyncio_loop_factories" hook to customize event loop creation.\
976+
"""
977+
965978

966979
def _parse_asyncio_marker(
967980
asyncio_marker: Mark,

tests/markers/test_class_scope.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ async def test_does_not_use_custom_event_loop_policy():
133133
pytest_args.extend(["-W", "default"])
134134
result = pytester.runpytest(*pytest_args)
135135
if sys.version_info >= (3, 14):
136-
result.assert_outcomes(passed=2, warnings=3)
136+
result.assert_outcomes(passed=2, warnings=4)
137137
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
138138
else:
139139
result.assert_outcomes(passed=2)
@@ -168,7 +168,7 @@ async def test_parametrized_loop(self, request):
168168
pytest_args.extend(["-W", "default"])
169169
result = pytester.runpytest(*pytest_args)
170170
if sys.version_info >= (3, 14):
171-
result.assert_outcomes(passed=2, warnings=2)
171+
result.assert_outcomes(passed=2, warnings=3)
172172
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
173173
else:
174174
result.assert_outcomes(passed=2)

tests/markers/test_function_scope.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async def test_uses_custom_event_loop_policy():
107107
pytest_args.extend(["-W", "default"])
108108
result = pytester.runpytest(*pytest_args)
109109
if sys.version_info >= (3, 14):
110-
result.assert_outcomes(passed=1, warnings=2)
110+
result.assert_outcomes(passed=1, warnings=3)
111111
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
112112
else:
113113
result.assert_outcomes(passed=1)
@@ -148,12 +148,57 @@ async def test_parametrized_loop():
148148
pytest_args.extend(["-W", "default"])
149149
result = pytester.runpytest(*pytest_args)
150150
if sys.version_info >= (3, 14):
151-
result.assert_outcomes(passed=2, warnings=3)
151+
result.assert_outcomes(passed=2, warnings=4)
152152
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
153153
else:
154154
result.assert_outcomes(passed=2)
155155

156156

157+
def test_event_loop_policy_fixture_override_emits_deprecation_warning(
158+
pytester: Pytester,
159+
):
160+
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
161+
pytester.makepyfile(
162+
dedent("""\
163+
import asyncio
164+
import pytest
165+
166+
pytestmark = pytest.mark.asyncio
167+
168+
@pytest.fixture
169+
def event_loop_policy():
170+
return asyncio.DefaultEventLoopPolicy()
171+
172+
async def test_anything():
173+
pass
174+
"""),
175+
)
176+
result = pytester.runpytest("--asyncio-mode=strict", "-W", "default")
177+
result.assert_outcomes(passed=1)
178+
result.stdout.fnmatch_lines(
179+
"*PytestDeprecationWarning*event_loop_policy*deprecated*"
180+
)
181+
182+
183+
def test_default_event_loop_policy_fixture_does_not_warn(
184+
pytester: Pytester,
185+
):
186+
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
187+
pytester.makepyfile(
188+
dedent("""\
189+
import pytest
190+
191+
pytestmark = pytest.mark.asyncio
192+
193+
async def test_anything():
194+
pass
195+
"""),
196+
)
197+
result = pytester.runpytest("--asyncio-mode=strict", "-W", "default")
198+
result.assert_outcomes(passed=1)
199+
result.stdout.no_fnmatch_line("*PytestDeprecationWarning*event_loop_policy*")
200+
201+
157202
def test_asyncio_mark_provides_function_scoped_loop_to_fixtures(
158203
pytester: Pytester,
159204
):

tests/markers/test_module_scope.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ async def test_does_not_use_custom_event_loop_policy():
8383
pytest_args.extend(["-W", "default"])
8484
result = pytester.runpytest(*pytest_args)
8585
if sys.version_info >= (3, 14):
86-
result.assert_outcomes(passed=2, warnings=3)
86+
result.assert_outcomes(passed=2, warnings=4)
8787
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
8888
else:
8989
result.assert_outcomes(passed=2)
@@ -118,7 +118,7 @@ async def test_parametrized_loop():
118118
pytest_args.extend(["-W", "default"])
119119
result = pytester.runpytest(*pytest_args)
120120
if sys.version_info >= (3, 14):
121-
result.assert_outcomes(passed=2, warnings=2)
121+
result.assert_outcomes(passed=2, warnings=3)
122122
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
123123
else:
124124
result.assert_outcomes(passed=2)

tests/markers/test_package_scope.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async def test_also_uses_custom_event_loop_policy():
115115
pytest_args.extend(["-W", "default"])
116116
result = pytester.runpytest(*pytest_args)
117117
if sys.version_info >= (3, 14):
118-
result.assert_outcomes(passed=2, warnings=3)
118+
result.assert_outcomes(passed=2, warnings=4)
119119
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
120120
else:
121121
result.assert_outcomes(passed=2)
@@ -153,7 +153,7 @@ async def test_parametrized_loop():
153153
pytest_args.extend(["-W", "default"])
154154
result = pytester.runpytest(*pytest_args)
155155
if sys.version_info >= (3, 14):
156-
result.assert_outcomes(passed=2, warnings=2)
156+
result.assert_outcomes(passed=2, warnings=3)
157157
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
158158
else:
159159
result.assert_outcomes(passed=2)

tests/markers/test_session_scope.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ async def test_also_uses_custom_event_loop_policy():
116116
pytest_args.extend(["-W", "default"])
117117
result = pytester.runpytest(*pytest_args)
118118
if sys.version_info >= (3, 14):
119-
result.assert_outcomes(passed=2, warnings=3)
119+
result.assert_outcomes(passed=2, warnings=4)
120120
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
121121
else:
122122
result.assert_outcomes(passed=2)
@@ -154,7 +154,7 @@ async def test_parametrized_loop():
154154
pytest_args.extend(["-W", "default"])
155155
result = pytester.runpytest(*pytest_args)
156156
if sys.version_info >= (3, 14):
157-
result.assert_outcomes(passed=2, warnings=2)
157+
result.assert_outcomes(passed=2, warnings=3)
158158
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
159159
else:
160160
result.assert_outcomes(passed=2)

0 commit comments

Comments
 (0)