Skip to content

Commit 3020c55

Browse files
committed
[refactor] Extracted function to iterate over the collector hierarchy of a test item.
Signed-off-by: Michael Seifert <[email protected]>
1 parent 84ec512 commit 3020c55

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

pytest_asyncio/plugin.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import socket
99
import warnings
1010
from asyncio import AbstractEventLoopPolicy
11+
from itertools import dropwhile
1112
from textwrap import dedent
1213
from typing import (
1314
Any,
@@ -958,18 +959,26 @@ def _retrieve_scope_root(item: Union[Collector, Item], scope: str) -> Collector:
958959
"package": Package,
959960
"session": Session,
960961
}
962+
collectors = _iter_collectors(item)
961963
scope_root_type = node_type_by_scope[scope]
962-
for node in reversed(item.listchain()):
963-
if isinstance(node, scope_root_type):
964-
assert isinstance(node, pytest.Collector)
965-
return node
964+
collector_with_specified_scope = next(
965+
dropwhile(lambda c: not isinstance(c, scope_root_type), collectors), None
966+
)
967+
if collector_with_specified_scope:
968+
return collector_with_specified_scope
966969
error_message = (
967970
f"{item.name} is marked to be run in an event loop with scope {scope}, "
968971
f"but is not part of any {scope}."
969972
)
970973
raise pytest.UsageError(error_message)
971974

972975

976+
def _iter_collectors(item: Union[Collector, Item]) -> Iterable[Collector]:
977+
for node in reversed(item.listchain()):
978+
if isinstance(node, pytest.Collector):
979+
yield node
980+
981+
973982
@pytest.fixture
974983
def event_loop(request: FixtureRequest) -> Iterator[asyncio.AbstractEventLoop]:
975984
"""Create an instance of the default event loop for each test case."""

0 commit comments

Comments
 (0)