Skip to content

Commit f035aad

Browse files
kumaraditya303bdraco
authored andcommitted
remove use of deprecated policy API from tests (#10851)
(cherry picked from commit e5d1240)
1 parent 99a2234 commit f035aad

File tree

7 files changed

+32
-29
lines changed

7 files changed

+32
-29
lines changed

CHANGES/10851.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed pytest plugin to not use deprecated :py:mod:`asyncio` policy APIs.

CHANGES/10851.contrib.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Updated tests to avoid using deprecated :py:mod:`asyncio` policy APIs and
2+
make it compatible with Python 3.14.

aiohttp/pytest_plugin.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
Iterator,
1111
Optional,
1212
Protocol,
13-
Type,
1413
Union,
1514
overload,
1615
)
@@ -208,9 +207,13 @@ def pytest_pyfunc_call(pyfuncitem): # type: ignore[no-untyped-def]
208207
"""Run coroutines in an event loop instead of a normal function call."""
209208
fast = pyfuncitem.config.getoption("--aiohttp-fast")
210209
if inspect.iscoroutinefunction(pyfuncitem.function):
211-
existing_loop = pyfuncitem.funcargs.get(
212-
"proactor_loop"
213-
) or pyfuncitem.funcargs.get("loop", None)
210+
existing_loop = (
211+
pyfuncitem.funcargs.get("proactor_loop")
212+
or pyfuncitem.funcargs.get("selector_loop")
213+
or pyfuncitem.funcargs.get("uvloop_loop")
214+
or pyfuncitem.funcargs.get("loop", None)
215+
)
216+
214217
with _runtime_warning_context():
215218
with _passthrough_loop_context(existing_loop, fast=fast) as _loop:
216219
testargs = {
@@ -227,11 +230,11 @@ def pytest_generate_tests(metafunc): # type: ignore[no-untyped-def]
227230
return
228231

229232
loops = metafunc.config.option.aiohttp_loop
230-
avail_factories: Dict[str, Type[asyncio.AbstractEventLoopPolicy]]
231-
avail_factories = {"pyloop": asyncio.DefaultEventLoopPolicy}
233+
avail_factories: dict[str, Callable[[], asyncio.AbstractEventLoop]]
234+
avail_factories = {"pyloop": asyncio.new_event_loop}
232235

233236
if uvloop is not None: # pragma: no cover
234-
avail_factories["uvloop"] = uvloop.EventLoopPolicy
237+
avail_factories["uvloop"] = uvloop.new_event_loop
235238

236239
if loops == "all":
237240
loops = "pyloop,uvloop?"
@@ -255,23 +258,24 @@ def pytest_generate_tests(metafunc): # type: ignore[no-untyped-def]
255258

256259

257260
@pytest.fixture
258-
def loop(loop_factory, fast, loop_debug): # type: ignore[no-untyped-def]
261+
def loop(
262+
loop_factory: Callable[[], asyncio.AbstractEventLoop],
263+
fast: bool,
264+
loop_debug: bool,
265+
) -> Iterator[asyncio.AbstractEventLoop]:
259266
"""Return an instance of the event loop."""
260-
policy = loop_factory()
261-
asyncio.set_event_loop_policy(policy)
262-
with loop_context(fast=fast) as _loop:
267+
with loop_context(loop_factory, fast=fast) as _loop:
263268
if loop_debug:
264269
_loop.set_debug(True) # pragma: no cover
265270
asyncio.set_event_loop(_loop)
266271
yield _loop
267272

268273

269274
@pytest.fixture
270-
def proactor_loop(): # type: ignore[no-untyped-def]
271-
policy = asyncio.WindowsProactorEventLoopPolicy() # type: ignore[attr-defined]
272-
asyncio.set_event_loop_policy(policy)
275+
def proactor_loop() -> Iterator[asyncio.AbstractEventLoop]:
276+
factory = asyncio.ProactorEventLoop # type: ignore[attr-defined]
273277

274-
with loop_context(policy.new_event_loop) as _loop:
278+
with loop_context(factory) as _loop:
275279
asyncio.set_event_loop(_loop)
276280
yield _loop
277281

tests/conftest.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,21 +230,17 @@ def _proto_factory(conn_closing_result=None, **kwargs):
230230

231231

232232
@pytest.fixture
233-
def selector_loop():
234-
policy = asyncio.WindowsSelectorEventLoopPolicy()
235-
asyncio.set_event_loop_policy(policy)
236-
237-
with loop_context(policy.new_event_loop) as _loop:
233+
def selector_loop() -> Iterator[asyncio.AbstractEventLoop]:
234+
factory = asyncio.SelectorEventLoop
235+
with loop_context(factory) as _loop:
238236
asyncio.set_event_loop(_loop)
239237
yield _loop
240238

241239

242240
@pytest.fixture
243241
def uvloop_loop() -> Iterator[asyncio.AbstractEventLoop]:
244-
policy = uvloop.EventLoopPolicy()
245-
asyncio.set_event_loop_policy(policy)
246-
247-
with loop_context(policy.new_event_loop) as _loop:
242+
factory = uvloop.new_event_loop
243+
with loop_context(factory) as _loop:
248244
asyncio.set_event_loop(_loop)
249245
yield _loop
250246

tests/test_connector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def create_mocked_conn(conn_closing_result=None, **kwargs):
107107
try:
108108
loop = asyncio.get_running_loop()
109109
except RuntimeError:
110-
loop = asyncio.get_event_loop_policy().get_event_loop()
110+
loop = asyncio.get_event_loop()
111111

112112
proto = mock.Mock(**kwargs)
113113
proto.closed = loop.create_future()

tests/test_loop.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def test_default_loop(self) -> None:
3737
self.assertIs(self.loop, asyncio.get_event_loop_policy().get_event_loop())
3838

3939

40-
def test_default_loop(loop) -> None:
41-
assert asyncio.get_event_loop_policy().get_event_loop() is loop
40+
def test_default_loop(loop: asyncio.AbstractEventLoop) -> None:
41+
assert asyncio.get_event_loop() is loop
4242

4343

4444
def test_setup_loop_non_main_thread() -> None:
@@ -47,7 +47,7 @@ def test_setup_loop_non_main_thread() -> None:
4747
def target() -> None:
4848
try:
4949
with loop_context() as loop:
50-
assert asyncio.get_event_loop_policy().get_event_loop() is loop
50+
assert asyncio.get_event_loop() is loop
5151
loop.run_until_complete(test_subprocess_co(loop))
5252
except Exception as exc:
5353
nonlocal child_exc

tests/test_proxy_functional.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ async def test_https_proxy_unsupported_tls_in_tls(
204204
await asyncio.sleep(0.1)
205205

206206

207-
@pytest.mark.usefixtures("uvloop_loop")
208207
@pytest.mark.skipif(
209208
platform.system() == "Windows" or sys.implementation.name != "cpython",
210209
reason="uvloop is not supported on Windows and non-CPython implementations",
@@ -216,6 +215,7 @@ async def test_https_proxy_unsupported_tls_in_tls(
216215
async def test_uvloop_secure_https_proxy(
217216
client_ssl_ctx: ssl.SSLContext,
218217
secure_proxy_url: URL,
218+
uvloop_loop: asyncio.AbstractEventLoop,
219219
) -> None:
220220
"""Ensure HTTPS sites are accessible through a secure proxy without warning when using uvloop."""
221221
conn = aiohttp.TCPConnector()

0 commit comments

Comments
 (0)