Skip to content

Commit 7c8fa82

Browse files
committed
refactor: Extracted function that returns the event loop scope of a marked function.
1 parent 9d4ad54 commit 7c8fa82

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

pytest_asyncio/plugin.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
FixtureRequest,
3939
Function,
4040
Item,
41+
Mark,
4142
Metafunc,
4243
Module,
4344
Package,
@@ -738,7 +739,7 @@ def pytest_generate_tests(metafunc: Metafunc) -> None:
738739
marker = metafunc.definition.get_closest_marker("asyncio")
739740
if not marker:
740741
return
741-
scope = marker.kwargs.get("scope", "function")
742+
scope = _get_marked_loop_scope(marker)
742743
if scope == "function":
743744
return
744745
event_loop_node = _retrieve_scope_root(metafunc.definition, scope)
@@ -970,7 +971,7 @@ def pytest_runtest_setup(item: pytest.Item) -> None:
970971
marker = item.get_closest_marker("asyncio")
971972
if marker is None:
972973
return
973-
scope = marker.kwargs.get("scope", "function")
974+
scope = _get_marked_loop_scope(marker)
974975
if scope != "function":
975976
parent_node = _retrieve_scope_root(item, scope)
976977
event_loop_fixture_id = parent_node.stash[_event_loop_fixture_id]
@@ -989,6 +990,11 @@ def pytest_runtest_setup(item: pytest.Item) -> None:
989990
)
990991

991992

993+
def _get_marked_loop_scope(asyncio_marker: Mark) -> _ScopeName:
994+
assert asyncio_marker.name == "asyncio"
995+
return asyncio_marker.kwargs.get("scope", "function")
996+
997+
992998
def _retrieve_scope_root(item: Union[Collector, Item], scope: str) -> Collector:
993999
node_type_by_scope = {
9941000
"class": Class,

0 commit comments

Comments
 (0)