Skip to content

Commit e5d1240

Browse files
remove use of deprecated policy API from tests (#10851)
1 parent 323bdcf commit e5d1240

File tree

7 files changed

+25
-25
lines changed

7 files changed

+25
-25
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: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,13 @@ def pytest_pyfunc_call(pyfuncitem): # type: ignore[no-untyped-def]
224224
"""Run coroutines in an event loop instead of a normal function call."""
225225
fast = pyfuncitem.config.getoption("--aiohttp-fast")
226226
if inspect.iscoroutinefunction(pyfuncitem.function):
227-
existing_loop = pyfuncitem.funcargs.get(
228-
"proactor_loop"
229-
) or pyfuncitem.funcargs.get("loop", None)
227+
existing_loop = (
228+
pyfuncitem.funcargs.get("proactor_loop")
229+
or pyfuncitem.funcargs.get("selector_loop")
230+
or pyfuncitem.funcargs.get("uvloop_loop")
231+
or pyfuncitem.funcargs.get("loop", None)
232+
)
233+
230234
with _runtime_warning_context():
231235
with _passthrough_loop_context(existing_loop, fast=fast) as _loop:
232236
testargs = {
@@ -243,11 +247,11 @@ def pytest_generate_tests(metafunc): # type: ignore[no-untyped-def]
243247
return
244248

245249
loops = metafunc.config.option.aiohttp_loop
246-
avail_factories: Dict[str, Type[asyncio.AbstractEventLoopPolicy]]
247-
avail_factories = {"pyloop": asyncio.DefaultEventLoopPolicy}
250+
avail_factories: dict[str, Callable[[], asyncio.AbstractEventLoop]]
251+
avail_factories = {"pyloop": asyncio.new_event_loop}
248252

249253
if uvloop is not None:
250-
avail_factories["uvloop"] = uvloop.EventLoopPolicy
254+
avail_factories["uvloop"] = uvloop.new_event_loop
251255

252256
if loops == "all":
253257
loops = "pyloop,uvloop?"
@@ -272,14 +276,12 @@ def pytest_generate_tests(metafunc): # type: ignore[no-untyped-def]
272276

273277
@pytest.fixture
274278
def loop(
275-
loop_factory: Callable[[], asyncio.AbstractEventLoopPolicy],
279+
loop_factory: Callable[[], asyncio.AbstractEventLoop],
276280
fast: bool,
277281
loop_debug: bool,
278282
) -> Iterator[asyncio.AbstractEventLoop]:
279283
"""Return an instance of the event loop."""
280-
policy = loop_factory()
281-
asyncio.set_event_loop_policy(policy)
282-
with loop_context(fast=fast) as _loop:
284+
with loop_context(loop_factory, fast=fast) as _loop:
283285
if loop_debug:
284286
_loop.set_debug(True)
285287
asyncio.set_event_loop(_loop)
@@ -288,10 +290,9 @@ def loop(
288290

289291
@pytest.fixture
290292
def proactor_loop() -> Iterator[asyncio.AbstractEventLoop]:
291-
policy = asyncio.WindowsProactorEventLoopPolicy() # type: ignore[attr-defined]
292-
asyncio.set_event_loop_policy(policy)
293+
factory = asyncio.ProactorEventLoop # type: ignore[attr-defined]
293294

294-
with loop_context(policy.new_event_loop) as _loop:
295+
with loop_context(factory) as _loop:
295296
asyncio.set_event_loop(_loop)
296297
yield _loop
297298

tests/conftest.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,20 +233,16 @@ def assert_sock_fits(sock_path: str) -> None:
233233

234234
@pytest.fixture
235235
def selector_loop() -> Iterator[asyncio.AbstractEventLoop]:
236-
policy = asyncio.WindowsSelectorEventLoopPolicy() # type: ignore[attr-defined]
237-
asyncio.set_event_loop_policy(policy)
238-
239-
with loop_context(policy.new_event_loop) as _loop:
236+
factory = asyncio.SelectorEventLoop
237+
with loop_context(factory) as _loop:
240238
asyncio.set_event_loop(_loop)
241239
yield _loop
242240

243241

244242
@pytest.fixture
245243
def uvloop_loop() -> Iterator[asyncio.AbstractEventLoop]:
246-
policy = uvloop.EventLoopPolicy()
247-
asyncio.set_event_loop_policy(policy)
248-
249-
with loop_context(policy.new_event_loop) as _loop:
244+
factory = uvloop.new_event_loop
245+
with loop_context(factory) as _loop:
250246
asyncio.set_event_loop(_loop)
251247
yield _loop
252248

tests/test_connector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def create_mocked_conn(
124124
try:
125125
loop = asyncio.get_running_loop()
126126
except RuntimeError:
127-
loop = asyncio.get_event_loop_policy().get_event_loop()
127+
loop = asyncio.get_event_loop()
128128

129129
f = loop.create_future()
130130
proto: mock.Mock = mock.create_autospec(

tests/test_loop.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async def test_on_startup_hook(self) -> None:
3737

3838

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

4242

4343
def test_setup_loop_non_main_thread() -> None:
@@ -46,7 +46,7 @@ def test_setup_loop_non_main_thread() -> None:
4646
def target() -> None:
4747
try:
4848
with loop_context() as loop:
49-
assert asyncio.get_event_loop_policy().get_event_loop() is loop
49+
assert asyncio.get_event_loop() is loop
5050
loop.run_until_complete(test_subprocess_co(loop))
5151
except Exception as exc:
5252
nonlocal child_exc

tests/test_proxy_functional.py

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

243243

244-
@pytest.mark.usefixtures("uvloop_loop")
245244
@pytest.mark.skipif(
246245
platform.system() == "Windows" or sys.implementation.name != "cpython",
247246
reason="uvloop is not supported on Windows and non-CPython implementations",
@@ -253,6 +252,7 @@ async def test_https_proxy_unsupported_tls_in_tls(
253252
async def test_uvloop_secure_https_proxy(
254253
client_ssl_ctx: ssl.SSLContext,
255254
secure_proxy_url: URL,
255+
uvloop_loop: asyncio.AbstractEventLoop,
256256
) -> None:
257257
"""Ensure HTTPS sites are accessible through a secure proxy without warning when using uvloop."""
258258
conn = aiohttp.TCPConnector()

0 commit comments

Comments
 (0)