Skip to content

Commit 8095cde

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 aae1fd7 commit 8095cde

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,
@@ -1009,18 +1010,26 @@ def _retrieve_scope_root(item: Union[Collector, Item], scope: str) -> Collector:
10091010
"package": Package,
10101011
"session": Session,
10111012
}
1013+
collectors = _iter_collectors(item)
10121014
scope_root_type = node_type_by_scope[scope]
1013-
for node in reversed(item.listchain()):
1014-
if isinstance(node, scope_root_type):
1015-
assert isinstance(node, pytest.Collector)
1016-
return node
1015+
collector_with_specified_scope = next(
1016+
dropwhile(lambda c: not isinstance(c, scope_root_type), collectors), None
1017+
)
1018+
if collector_with_specified_scope:
1019+
return collector_with_specified_scope
10171020
error_message = (
10181021
f"{item.name} is marked to be run in an event loop with scope {scope}, "
10191022
f"but is not part of any {scope}."
10201023
)
10211024
raise pytest.UsageError(error_message)
10221025

10231026

1027+
def _iter_collectors(item: Union[Collector, Item]) -> Iterable[Collector]:
1028+
for node in reversed(item.listchain()):
1029+
if isinstance(node, pytest.Collector):
1030+
yield node
1031+
1032+
10241033
@pytest.fixture
10251034
def event_loop(request: FixtureRequest) -> Iterator[asyncio.AbstractEventLoop]:
10261035
"""Create an instance of the default event loop for each test case."""

0 commit comments

Comments
 (0)