Skip to content

Commit 30a550a

Browse files
committed
Use Item.ihook instead of Config.hook
1 parent 72646b4 commit 30a550a

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

pytest_asyncio/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def _collect_hook_loop_factories(
246246
config: Config,
247247
item: Item,
248248
) -> dict[str, LoopFactory] | None:
249-
hook_caller = config.hook.pytest_asyncio_loop_factories
249+
hook_caller = item.ihook.pytest_asyncio_loop_factories
250250
if not hook_caller.get_hookimpls():
251251
return None
252252

tests/test_loop_factory_parametrization.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,47 @@ def pytest_asyncio_loop_factories(config, item):
352352
return {"sub": SubCustomEventLoop}
353353
"""),
354354
)
355+
subdir.joinpath("test_sub.py").write_text(
356+
dedent("""\
357+
import asyncio
358+
import pytest
359+
360+
pytest_plugins = "pytest_asyncio"
361+
362+
@pytest.mark.asyncio
363+
async def test_uses_sub_loop():
364+
assert type(asyncio.get_running_loop()).__name__ == "SubCustomEventLoop"
365+
"""),
366+
)
367+
result = pytester.runpytest("--asyncio-mode=strict")
368+
result.assert_outcomes(passed=1)
369+
370+
371+
def test_nested_conftest_hook_respects_conftest_locality(
372+
pytester: Pytester,
373+
) -> None:
374+
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
375+
pytester.makeconftest(dedent("""\
376+
import asyncio
377+
378+
class RootCustomEventLoop(asyncio.SelectorEventLoop):
379+
pass
380+
381+
def pytest_asyncio_loop_factories(config, item):
382+
return {"root": RootCustomEventLoop}
383+
"""))
384+
subdir = pytester.mkdir("subdir")
385+
subdir.joinpath("conftest.py").write_text(
386+
dedent("""\
387+
import asyncio
388+
389+
class SubCustomEventLoop(asyncio.SelectorEventLoop):
390+
pass
391+
392+
def pytest_asyncio_loop_factories(config, item):
393+
return {"sub": SubCustomEventLoop}
394+
"""),
395+
)
355396
pytester.makepyfile(
356397
test_root=dedent("""\
357398
import asyncio
@@ -360,8 +401,10 @@ def pytest_asyncio_loop_factories(config, item):
360401
pytest_plugins = "pytest_asyncio"
361402
362403
@pytest.mark.asyncio
363-
async def test_uses_sub_loop():
364-
assert type(asyncio.get_running_loop()).__name__ == "SubCustomEventLoop"
404+
async def test_root_uses_root_loop():
405+
assert (
406+
type(asyncio.get_running_loop()).__name__ == "RootCustomEventLoop"
407+
)
365408
"""),
366409
)
367410
subdir.joinpath("test_sub.py").write_text(
@@ -372,7 +415,7 @@ async def test_uses_sub_loop():
372415
pytest_plugins = "pytest_asyncio"
373416
374417
@pytest.mark.asyncio
375-
async def test_uses_sub_loop():
418+
async def test_sub_uses_sub_loop():
376419
assert type(asyncio.get_running_loop()).__name__ == "SubCustomEventLoop"
377420
"""),
378421
)

0 commit comments

Comments
 (0)